Cakephp 2.8.4忽略hasMany [英] Cakephp 2.8.4 ignoring hasMany
问题描述
这是一个非常奇怪的问题,我一直没有能够找出很长一段时间。
我有一个模型上传, hasMany 都被 UploadModel 所忽略。
例如。 上传 hasMany 面。
在评论表上有一个名为foreign_key的字段,
所以UploadModel中的关系看起来像这样:
$ hasMany = ['Face'=> [foreignKey=> 'Face.upload_id']];
在包含Face +条件的上传中执行查找 [Face.celebrity_id = 4018] 我收到以下错误,因为查询缺少Left Join to Face:
$ media = $ this- > Upload-> find('all',array(
'conditions'=> array(
'Face.celebrity_id'=> 4018
),
' contains'=> array(
'Face'
)
));
SQL查询:
SELECT`Upload`.`id`,`Upload`.`user_id`,`Upload`.`filename`,`Upload`.`created`
FROM`uploads` AS`upload`
WHERE`Face`.`celebrity_id` = 4018;
SQL错误:
'where子句'中的未知列'Face.celebrity_id'
正如你可以看到的查询缺少左边加入到Face,这是所有的问题
$ hasMany,我在$ belongsTo中添加Face关系,它添加关系到查询,它的工作原理!
但是上传可以有很多面孔和面。 upload_id具有要上传的foreignkey,所以它需要一个母亲***** HASMANY ... lol
正如你可以看到,这是可怕的,我已经绝望了,我已绕过这个问题,通过在每个查询之前添加一个bindModel(),但它是完全不必要的,hasMany应该工作! f **** !!!!!
我想让Cake为我执行的查询是:
SELECT`Upload`.`id`,`Upload`.`filename` FROM`uploads` AS`Upload`
LEFT JOIN`upload_faces` AS`Face` ON (`Upload`.id =`Face`.`upload_id`)
WHERE`Face`.`celebrity_id` = 4018
$ b $
code> test()函数,在上传
表上运行查询。因此,它不能匹配您的查询中的 Face.celebrity_id
字段。您首先知道的两件事:- 条件是您在
条件
应用于与您的模型匹配的表。在您的情况下,上传
是执行查询的表,您的表不包含字段Face.celebrity_id
。 - $ hasMany可创建应用程序级关系(关联)。所以通过进行查询,你已经写在
test()
函数,它不加入查询结果。
您可以做的是
$ this-> find('all',array(
'contains'=&array; array(
'Face'=> array('conditions'=>'Face.celebrity_id = 4018'))))
$ b
这将返回 / code>为
4018
表的
face
/ code>,否则 null
数组。
希望这将帮助你,如果你想只返回那些与 Face.celebrity_id = 4018
,首先你必须在 face
表上运行查询,然后在第一个查询结果上运行另一个。
有关第二种情况的详细信息,您可以参考 this 。
This is a very odd issue that I have not been able to figure out for a very long time.
I have a model Upload, with several hasMany all being ignored by UploadModel.
For example. Upload hasMany Face.
On comments table I have a field called foreign_key that hosts the Upload.id. So the relationship in UploadModel looks like this:
$hasMany = ['Face' => ['foreignKey' => 'Face.upload_id']];
When performing a find in Upload with contain Face + conditions [Face.celebrity_id = 4018] I get the following error because the query is missing the Left Join to Face:
$media = $this->Upload->find('all',array(
'conditions' => array(
'Face.celebrity_id' => 4018
),
'contain' => array(
'Face'
)
));
SQL Query:
SELECT `Upload`.`id`, `Upload`.`user_id`, `Upload`.`filename`, `Upload`.`created`
FROM `uploads` AS `Upload`
WHERE `Face`.`celebrity_id` = 4018;
SQL Error:
Unknown column 'Face.celebrity_id' in 'where clause'
As you can see the query is missing the left joing to Face, that's all the problem
If instead of in the $hasMany, I add the Face relationship in $belongsTo, it adds the relationship to the query and it works!
But an Upload can have many Faces, and Face.upload_id has the foreignKey to Upload, so IT NEEDS TO BE A motherf***** HASMANY... lol
As you can see, this is terrible, I am already desperate, I have bypassed the issue by adding a bindModel() before each query, but it is totally unnecessary, the hasMany should work!!!! f****!!!!!
The query that I want Cake to perform for me is this:
SELECT `Upload`.`id`, `Upload`.`filename` FROM `uploads` AS `Upload`
LEFT JOIN `upload_faces` AS `Face` ON (`Upload`.id = `Face`.`upload_id`)
WHERE `Face`.`celebrity_id` = 4018
I appreciate any help, thanks.
In your test()
function, you run query on upload
table. So it can not match Face.celebrity_id
field from your query. Two things you have know first:
- Condition you are writing in
conditions
clause is applied on the table matching with your model. In your case,upload
is the table on which query is executed and your table contains no fieldFace.celebrity_id
. - $hasMany creates application level relations(associations). So by doing query like you have written in
test()
function, it doesn't join the query results.
What you can do is,
$this->find('all', array(
'contain' => array(
'Face' => array('conditions' => 'Face.celebrity_id = 4018'))))
This will return all rows of upload
table with associated row of face
table if celebrity_id
is 4018
, otherwise null
array.
Hope this will help you and if you want that only those many rows will be returned which are associated with Face.celebrity_id = 4018
, first you have to run query on face
table and then another on first query result.
For more detail of second case you can refer this.
这篇关于Cakephp 2.8.4忽略hasMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!