ZF2 - How to add to favorites?

I am trying to do an insert to select from a table method in zf2.

I am using the following procedure as an example.

How to execute INSERT INTO SELECT query in ZF2

My table method is contained in the table class in ZF2, which is a separate table in the db from the tables I am trying to insert.

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);
    }

      

Neither

$ Insert-> values ​​($ select)

or

$ Insert-> select ($ select)

Job...

The first attempt gives an error that $ select must be an array, whereas $ insert-> select ($ select) gives an error that select () is not an Insert method.

How can I get this request to work?

+3


source to share


1 answer


The Zend Framework feature you referenced was introduced in version 2.3 :

Zend\Db\Sql\Insert

can use an object Select

as a value source ( INSERT INTO ... SELECT

)



I suspect you are using an older version. You can compare Zend\Db\Sql\Insert

in v2.2.10 with v2.3.0 to see various exceptions thrown by invalid arguments.

You must update Zend Framework to version 2.3 or higher to use this feature.

+1


source







All Articles