[英]Cakephp 2.8.4 ignoring hasMany
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 . 我有一个模型Upload ,有几个hasMany被UploadModel忽略了。
For example. 例如。 Upload hasMany Face .
上传 hasMany Face 。
On comments table I have a field called foreign_key that hosts the Upload.id. 在评论表上,我有一个名为Foreign_key的字段,用于承载Upload.id。 So the relationship in UploadModel looks like this:
因此,UploadModel中的关系如下所示:
$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: 在包含上载面孔+条件[Face.celebrity_id = 4018]的上载中执行查找时,出现以下错误,因为查询缺少左连接面孔:
$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'
If instead of in the $hasMany, I add the Face relationship in $belongsTo, it adds the relationship to the query and it works! 如果不是在$ hasMany中,而是在$ belongsTo中添加Face关系,它将关系添加到查询中就可以了!
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 但是一个Upload可以有很多面孔,而Face.upload_id具有要上传的ForeignKey,因此它需要成为一个母亲***** HASMANY ...大声笑
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!!!! 如您所见,这太可怕了,我已经绝望了,我通过在每个查询之前添加一个bindModel()绕过了这个问题,但这完全没有必要,hasMany应该可以工作! f****!!!!!
F****!!!!!
The query that I want Cake to perform for me is this: 我希望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
I appreciate any help, thanks. 感谢您的帮助,谢谢。
好的,所以基本上,它并没有忽略hasMany,但是我不能在hasMany关系上使用条件,只有当它是hasOne时才能完成
In your test()
function, you run query on upload
table. 在您的
test()
函数中,您对upload
表运行查询。 So it can not match Face.celebrity_id
field from your query. 因此它不能匹配查询中的
Face.celebrity_id
字段。 Two things you have know first: 您首先要了解两件事:
conditions
clause is applied on the table matching with your model. conditions
子句中编写的conditions
将应用于与您的模型匹配的表。 In your case, upload
is the table on which query is executed and your table contains no field Face.celebrity_id
. upload
是执行查询的表,并且该表不包含Face.celebrity_id
字段。 test()
function, it doesn't join the query results. test()
函数那样进行查询,就不会加入查询结果。 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. 如果
celebrity_id
为4018
,则将返回upload
表的所有行以及相关的人face
表行,否则返回null
数组。
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. 希望这对您有帮助,如果您只希望返回与
Face.celebrity_id = 4018
相关联的许多行,则首先必须在人face
表上运行查询,然后在第一个查询结果上运行查询。
For more detail of second case you can refer this . 有关第二种情况的更多详细信息,请参阅此 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.