tableGateway を使用して次の SQL クエリをデータベースに渡すことは可能ですか?
UPDATE table_data SET active = not active where table_data.id = 12;
tableGateway を使用して次の SQL クエリをデータベースに渡すことは可能ですか?
UPDATE table_data SET active = not active where table_data.id = 12;
を使用する必要がありますZend\Db\Sql\Expression
。このクラスはZend\Db
、自分が何をしているのかを知っていること、およびこのクラスに渡された文字列の内容を変換するべきではなく、そのまま使用するべきであることを示しています。
// build the table gateway object
$adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$tableIdentifier = new TableIdentifier('table_data', 'public');
$tableGateway = new TableGateway($tableIdentifier, $adapter);
// create the filter
$where = new \Zend\Db\Sql\Where();
$where->equalTo('id', '12');
// update table
$tableGateway->update(
['active' => new \Zend\Db\Sql\Expression('not active')],
$where
);