如何使用CakePHP 3.x截断表格
我得到了截断查询:
$this->Coupons->schema()->truncateSql($this->Coupons->connection());
但执行它的最佳做法是什么
答案 0 :(得分:1)
这段代码效果很好,感谢this的评论,这有助于让答案更好。
//In Coupons Controller
$this->Coupons->connection()->transactional(function ($conn) {
$sqls = $this->Coupons->schema()->truncateSql($this->Coupons->connection());
foreach ($sqls as $sql) {
$this->Coupons->connection()->execute($sql)->execute();
}
});
答案 1 :(得分:1)
在 CakePHP4 中,您可以使用以下代码截断表:
$table = $this->Coupons;
$sqls = $table->getSchema()->truncateSql($table->getConnection());
foreach ($sqls as $sql) {
$table->getConnection()->execute($sql)->execute();
}
答案 2 :(得分:0)
我测试了以下内容,并且有效:
$connection = $this->Coupons->getConnection();
$connection->query('TRUNCATE coupons');
参考:https://book.cakephp.org/4/en/orm/database-basics.html#executing-queries
在此处了解更多信息:https://book.cakephp.org/4/en/orm/database-basics.html#using-transactions