问题描述
我正在使用 ModelA hasMany ModelB 开发插件。我可以通过简单的saveAll在两个模型中正确保存所有数据。
I'm developing a Plugin with ModelA hasMany ModelB. I can save correctly all data with a simply saveAll across the two models.
但是当我尝试在两个模型之间执行一个简单的查询时,发生了奇怪的事情。
But when I try to execute a simple query across the two models something strange happens.
$ModelA = new ModelA();
$result = $ModelA->find('all', array(
'recursive' => 1
));
pr($result);
Array
(
[0] => Array
(
[ModelA] => Array
(
[id] => 1
[field] => value
)
[ModelB] => Array
(
[0] => Array
(
[id] => 1
[model_a_id] => 1
)
[1] => Array
(
[id] => 2
[model_a_id] => 1
)
)
)
)
但在ModelB上添加条件不起作用:
But adding a condition on ModelB don't work:
$ModelA = new ModelA();
$result = $ModelA->find('all', array(
'recursive' => 1,
'conditions' => array(
'ModelB.id' => 2
)
));
pr($result);
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ModelB.id' in 'where clause'
但这项工作:
But this work:
$ModelA = new ModelA();
$result = $ModelA->ModelB->find('all', array(
'recursive' => 1,
'conditions' => array(
'ModelA.field' => 'value'
)
));
pr($result);
Array
(
[0] => Array
(
[ModelB] => Array
(
[id] => 2
[model_a_id] => 1
)
[ModelA] => Array
(
[id] => 1
[field] => value
)
)
)
有人有想法吗?感谢!
推荐答案
分隔查询
CakePHP使用单独的查询 hasMany
和 HABTM
关联。第一个检索主模型数据但不加入相关联的表,而第二个使用来自第一查询结果的ID来获取相关联的记录,然后将其合并到结果中。
Separate queries
CakePHP uses separate queries for hasMany
and HABTM
associations. The first one retrieves the main model data but doesn't join in associated tables, while the second one uses the IDs from the first queries results to fetch the associated records, which are then merged into the results.
现在的问题是,你的条件将被应用到第一个查询,其中不包括关联模型的表,因此错误。
Now the problem is that your conditions will be applied to the first query where the table of the associated model isn't included, hence the error.
另一种方法,即在 ModelB
上查找,它是一个 belongsTo
关联,其中只有一个查询
The other way around, ie a find on ModelB
, it's a belongsTo
association, where only one query is being used and the other table is joined into that query, and so the condition applies just fine.
虽然在您的具体示例中更容易简单地查询 ModelB
,但是这个问题也可以通过使用手动定义的联接来解决,如菜单中所述:
While it's easier in your specific example to simply query ModelB
instead, this problem can also be solved by using manually defined joins, as described in the Cookbook:
strong>
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
$ModelA->find('all', array(
'joins' => array(
array(
'table' => 'model_bs',
'alias' => 'ModelB',
'type' => 'LEFT',
'conditions' => array('ModelB.id = ModelA.model_b_id')
)
),
'conditions' => array(
'ModelB.id' => 2
)
));
这样, model_bs
第一个查询,你可以安全地测试 ModelB
的条件。
This way the model_bs
table is joined into the first query and you can safely test for conditions on ModelB
.
请注意,如果你不想实际上检索 ModelB
(这将再次是一个额外的查询)的数据,你需要设置 recursive
到 -1
。
Note that if you wouldn't want to actually retrieve the data of ModelB
(which would be an additional query again), you'd need to set recursive
to -1
.
如 new Model()
,使用 ClassRegistry :: init()
, Controller :: loadModel ()
,或在 Controller :: $ uses
中定义要加载的模型。
Never instantiate models like new Model()
, use ClassRegistry::init()
, Controller::loadModel()
, or define the models to load in Controller::$uses
.
这篇关于跨插件的两个模型的查询错误 - CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!