在CakePHP中显示查询而不运行它们

时间:2014-05-05 06:18:46

标签: cakephp

有没有办法运行模型命令,如$ this-> MyModel-> saveall($ rows)但没有它实际上对数据库执行操作,只显示它将运行的所有查询,方式当其中一个查询出错时会发生什么?

1 个答案:

答案 0 :(得分:2)

是的,你可以,看看"交易" http://book.cakephp.org/2.0/en/models/transactions.html

// get the datasource and store it in a local variable
$ds = $this->MyModel->getDataSource();
// begin a "transaction"
$ds->begin();
// do your saving
$this->MyModel->saveAll($rows); // you can add more queries here, that's what transactions are all about! :)
// rollback, in a normal situation you would check if the save was successful and commit()/rollbac() depending on the situation.
$ds->rollback();

请注意:自动增量字段增加,因为MySQL或任何其他数据库引擎将"保留"这些ID在进行交易时是为了防止重复的ID。这不应该是任何问题,但是当你正在调试并且你正在记住一个ID时,如果它是星期一早上(在那里,那样做的话)会让你头疼...... ;-)