简体   繁体   English

Cakephp 2.8.4忽略hasMany

[英]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 ,有几个hasManyUploadModel忽略了。

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'

As you can see the query is missing the left joing to Face, that's all the problem 如您所见,查询缺少对Face的左向导,仅此而已

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: 您首先要了解两件事:

  1. Condition you are writing in 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字段。
  2. $hasMany creates application level relations(associations). $ hasMany创建应用程序级别关系(关联)。 So by doing query like you have written in 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_id4018 ,则将返回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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM