0
votes

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.

2

2 Answers

0
votes

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

0
votes

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.