Kim*_*cks 2 mysql postgresql cakephp transactions cakephp-3.0
我正在使用CakePHP 3.x.
但是,我的一些execute语句需要先前insert语句的id.
如何在选择CakePHP 3.x并允许PostgreSQL或MySQL作为我的数据库时在事务中执行该操作?
我期望的查询是这样的:
INSERT INTO dynamic_views (`title`, `created`) values ('view 37', NOW());
-- take the id of the above insert query and assuming the view id is 37
INSERT INTO users_access_dynamic_views(user_id, view_id) values (44, 37);
Run Code Online (Sandbox Code Playgroud)
我想使用CakePHP3.x使用事务执行上述操作.
同时,我想问一下,对于上述情况使用交易是否被认为是个坏主意?
执行插入查询后,可以lastInsertId()使用正确的参数调用以获取正确的值:
$connection->transactional(function ($connection) {
$statement = $connection->newQuery
->insert($fields)
->into($table)
->values($values)
->execute();
$insertId = $statement->lastInsertId($table, 'id'); // id is primary key
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1877 次 |
| 最近记录: |