Cakephp 2.8.4 ignoring hasMany

╄→гoц情女王★ 提交于 2019-12-25 07:38:18

问题


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.


回答1:


ok, so basically, it is not ignoring the hasMany, but I can not use conditions on a hasMany relationship, can only be done if it is a hasOne




回答2:


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:

  1. 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 field Face.celebrity_id.
  2. $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.



来源:https://stackoverflow.com/questions/37946705/cakephp-2-8-4-ignoring-hasmany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!