Cakephp3事务查找查询
Cakephp3 transaction find query
在 cakephp3 中进行交易并在其中添加 get() 查询时一切正常。但是为什么在事务内部没有执行find()查询?
我在 cakephp3 中有以下控制器:
<?php
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\Network\Session;
use Cake\Event\Event;
use Cake\Network\Http\Client;
use Cake\ORM\TableRegistry;
use Cake\Datasource\ConnectionManager;
class DashboardController extends AppController {
public function index(){
$conn = ConnectionManager::get('default');
$testModel = TableRegistry::get('Tests');
$select1=array();
$select2 = array();
$saved = array();
$conn->transactional(function ($connection)use(&$testModel,&$select1,&$select2,&$saved) {
$select1 = $testModel->find('all')->where(['id' => 2]); // article with id 12
$select2 = $testModel->get(1);
$select2->content = 'foo';
$saved = $testModel->save($select2);
});
}
}
?>
我希望在 SQL-Log 中得到这个:
BEGIN
SELECT Tests.id AS `Tests__id`, Tests.content AS `Tests__content` FROM tests Tests WHERE id = 2
SELECT Tests.id AS `Tests__id`, Tests.content AS `Tests__content` FROM tests Tests WHERE Tests.id = 1 LIMIT 1
UPDATE tests SET content = 'foo' WHERE id = 1
COMMIT
但我得到的是:
BEGIN
SELECT Tests.id AS `Tests__id`, Tests.content AS `Tests__content` FROM tests Tests WHERE Tests.id = 1 LIMIT 1
UPDATE tests SET content = 'foo' WHERE id = 1
COMMIT
SELECT Tests.id AS `Tests__id`, Tests.content AS `Tests__content` FROM tests Tests WHERE id = 2
因为它对我来说工作正常,我怀疑你在事务回调之外还有一个额外的查询,它也会查找 id = 2
,因为查询是由你显示的 find()
生成的永远不会执行调用,因为正在对查询进行惰性评估。
现在您刚刚创建了一个查询对象,为了实际执行它,您必须调用 all()
、first()
、toArray()
等。 ., 或遍历它。
另见 Cookbook > ...ORM > Query Builder > How Are Queries Lazily Evaluated
在 cakephp3 中进行交易并在其中添加 get() 查询时一切正常。但是为什么在事务内部没有执行find()查询?
我在 cakephp3 中有以下控制器:
<?php
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\Network\Session;
use Cake\Event\Event;
use Cake\Network\Http\Client;
use Cake\ORM\TableRegistry;
use Cake\Datasource\ConnectionManager;
class DashboardController extends AppController {
public function index(){
$conn = ConnectionManager::get('default');
$testModel = TableRegistry::get('Tests');
$select1=array();
$select2 = array();
$saved = array();
$conn->transactional(function ($connection)use(&$testModel,&$select1,&$select2,&$saved) {
$select1 = $testModel->find('all')->where(['id' => 2]); // article with id 12
$select2 = $testModel->get(1);
$select2->content = 'foo';
$saved = $testModel->save($select2);
});
}
}
?>
我希望在 SQL-Log 中得到这个:
BEGIN
SELECT Tests.id AS `Tests__id`, Tests.content AS `Tests__content` FROM tests Tests WHERE id = 2
SELECT Tests.id AS `Tests__id`, Tests.content AS `Tests__content` FROM tests Tests WHERE Tests.id = 1 LIMIT 1
UPDATE tests SET content = 'foo' WHERE id = 1
COMMIT
但我得到的是:
BEGIN
SELECT Tests.id AS `Tests__id`, Tests.content AS `Tests__content` FROM tests Tests WHERE Tests.id = 1 LIMIT 1
UPDATE tests SET content = 'foo' WHERE id = 1
COMMIT
SELECT Tests.id AS `Tests__id`, Tests.content AS `Tests__content` FROM tests Tests WHERE id = 2
因为它对我来说工作正常,我怀疑你在事务回调之外还有一个额外的查询,它也会查找 id = 2
,因为查询是由你显示的 find()
生成的永远不会执行调用,因为正在对查询进行惰性评估。
现在您刚刚创建了一个查询对象,为了实际执行它,您必须调用 all()
、first()
、toArray()
等。 ., 或遍历它。
另见 Cookbook > ...ORM > Query Builder > How Are Queries Lazily Evaluated