zend Framework 2 通过 TableGateway 对象更新查询
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();
}
我的输出打印:158123
它让我在我已经作为参数传递的 set() 函数中传递数组。我也尝试将对象转换为数组 ((arrya)$objetc) 但它对我不起作用。
[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
试试看,
我遇到了同样的问题,我试过这个,它奏效了。
$rowset = $projectTable->update(array('statement_no' => '01010'), $where);
您可以通过实施 Zend\Db\Sql\Update
对象来实现。您可以使用 TableGateway
创建该对象。您应该能够在您的模型中执行以下操作
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);
}
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();
}
我的输出打印:158123 它让我在我已经作为参数传递的 set() 函数中传递数组。我也尝试将对象转换为数组 ((arrya)$objetc) 但它对我不起作用。
[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
试试看, 我遇到了同样的问题,我试过这个,它奏效了。
$rowset = $projectTable->update(array('statement_no' => '01010'), $where);
您可以通过实施 Zend\Db\Sql\Update
对象来实现。您可以使用 TableGateway
创建该对象。您应该能够在您的模型中执行以下操作
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);
}