0

是否可以使用 tableGateway 将以下 sql 查询传递给数据库,如果是这样,这样的命令会是什么样子?

UPDATE table_data SET active = not active where table_data.id = 12;
4

1 回答 1

0

你需要使用 a 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
);
于 2015-05-07T12:50:07.320 回答