Search code examples
phpzend-frameworkzend-framework2zend-dbtablegateway

zend Framework 2 update query by TableGateway Object


public function update($table, $where = array(), $data_arr = array()){

    print_r($data_arr);

    $adapter = $this->tableGateway->getAdapter();

    $projectTable;

    if($table != null){
        $projectTable = new TableGateway($table, $adapter);
    }else{
        $projectTable = new TableGateway('account_master', $adapter);
    }
    echo "158";

    try {
        echo "123";
        $rowset = $projectTable->update(function(Update $update) use ($where, $data_arr) {

                $update->set(array('statement_no' => '01010'));

                $update->where($where);

            echo $update->getSqlString();
        });
    } catch (\Exception $e) {
            print_r($e);
    }

    print_r($rowset);
    die();
}

my Output print : 158123 it's give me pass array in set() function that i already pass as argument. also i have tried to convert object to array ((arrya)$objetc) but it's not work for me.

[10-Jul-2017 05:11:34 America/Denver] PHP Catchable fatal error:  Argument 1 passed to Zend\Db\Sql\Update::set() must be of the type array, object given, called in /home2/flywing1/vendor/zendframework/zend-db/src/TableGateway/AbstractTableGateway.php on line 336 and defined in /home2/flywing1/vendor/zendframework/zend-db/src/Sql/Update.php on line 93

Solution

  • You may do that by implementing a Zend\Db\Sql\Update object. You may create that object using the TableGateway. You should be able to do the following in your model

    public function update($set, $where)
    {
        // Here is the catch  
        $update = $this->tableGateway->getSql()->update();
        $update->set($set);
        $update->where($where);
    
        // Execute the query
        return $this->tableGateway->updateWith($update);
    }