ZF2 - 如何插入 select?

ZF2 - How do I insert into select?

我正在尝试从 zf2 中的 table 方法向 select 执行插入。

下面的过程是我拿来举例的

How to perform an INSERT INTO SELECT query in ZF2

我的 table 方法包含在 ZF2 的 table class 中,它是数据库中与我的 tables 分开的 table试图插入。

public function addCategoryName($val)
    {
        $data = array ( 
            'CategoryName' => $val);

        $this->tableGateway->insert($data);
        $id = $this->tableGateway->lastInsertValue;

        $on = "categoryid = $id";
        $select = new Select('actionitems');

        $select->columns(array('actionitemid', 'categoryid'))->join('categories', $on, array('actionitemid', 'categoryid'), 'cross'); 

        $adapterInsert = new \Zend\Db\Adapter\Adapter(array(
            'driver'   => 'pdo_mysql',
            'database' => 'actionitemmanager',
            'username' => 'username',
            'password' => 'password'
        ));

        $insert = new Insert();
        $insert->into('actionitemcategories');
        $insert->columns(array('actionitemid', 'categoryid'));
        $insert->values($select);
        //insert with select
        $adapterInsert->insertWith($insert);
    }

都没有

$insert->values($select)

$insert->select($select)

工作...

第一次尝试给出了 $select 必须是数组的错误,而 $insert->select($select) 给出了 select() 不是 Insert 的方法。

我怎样才能让这个查询起作用?

您引用的 Zend Framework 功能是 introduced in v2.3:

Zend\Db\Sql\Insert can use a Select object as the value source (INSERT INTO ... SELECT)

我怀疑您使用的是旧版本。您可以比较 Zend\Db\Sql\Insertv2.2.10 to v2.3.0 以查看针对无效参数抛出的不同异常。

您应该将 Zend Framework 升级到 v2.3 或更高版本才能使用此功能。