1

I am trying with the following line of code to filter all the Posts that have certain (irrelevant) conditions, by the tag "who"

$com= $this->Post->find('all', array('conditions' => $conditions, 'contain' => 'Tag.tag="who"'));

However, instead of the results being only the posts with the tags, I get every single post with empty tag arrays for the ones that don't have the tag "who".

I know my question has come up before, but the solutions posted have not worked with my code. I have tried adapting the code here to my own: http://web-development-blog.co.uk/2010/09/14/cakephp-habtm-find-with-conditions-and-containable-behavior/, but I get an SQL error stating "Tag.post_id" not found in on clause.

Please help.

Error message when trying to implement code from selected link:

SELECT `Post`.`id`, `Post`.`title`, `Post`.`body`, `Post`.`created`,       `Post`.`modified`, `Tag`.`id`, `Tag`.`tag`, `Tag`.`created`, `Tag`.`modified` FROM `posts` AS `Post` LEFT JOIN `tags` AS `Tag` ON (`Tag`.`post_id` = `Post`.`id`) WHERE `Tag`.`tag` = 'who'    1054: Unknown column 'Tag.post_id' in 'on clause'            

Which is caused by using this:

$this->Post->bindModel(array('hasOne' => array('Tag')));
$this->Post->contain(array(
  'Tag'
));
$com=$this->Post->find('all', array(
  'conditions'=>array('Tag.tag'=>'who') 
)); 
user794486
  • 31
  • 2

1 Answers1

0

Your error is that you are using bindModel wihtout saying the foreignkey or anything, you SHOULD establish the associations in the model and just use this on-the-fly method when is strictly neccesary.

you need to tell cake which foreignkey of not cake will use the default, in your case it will try Tag.post_id.

Also you should use a has many association or at least i think so. I said this beacause i suppose a post may have multiple tags.

once you do the asociation in the model and delete this line

$this->Post->bindModel(array('hasOne' => array('Tag')));

it should work perfectly

OR you can do the association with the correct parameters on the fly read the cookbook for this, the example that shows how to put the classname is the one you seek, just add the other attributes

EDIT:

Sorry, read something wrong... the has many association doesn't do a join so you have to use contain to specify the condition or add a condition to the association on the fly, here is how your code should look after adding the conditions with contain

$contain = array(
  'Tag' => array(
       'conditions'=> array('Tag.tag'=>'who') 
   )
);
$com=$this->Post->find('all', array(
  'contain'=>$contain 
));

REMEMBER contain conditions override the conditions in the associations!! if you want to have extra conditions you must add them on the fly something like:

$this->Post->hasMany['Tag']['conditions'] += array('Tag.tag'=>'who');

Remember that associations on the fly will work for that instance ONLY.

Also you may do a hasmany alias for different types (remember to define always the classname and foreignkey).

Hope it works this time, if not just comment to see it fix ;)

api55
  • 11,070
  • 4
  • 41
  • 57
  • I am using a hasmany association. What I think was your recommendation resulted in this error: 'Query: SELECT `Post`.`id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified` FROM `posts` AS `Post` WHERE `Tag`.`tag` = 'who' 1054: Unknown column 'Tag.tag' in 'where clause' ' – user794486 Oct 05 '11 at 01:19
  • @user794486 oh, i saw something wrong XD img going to edit my question with the solution. – api55 Oct 05 '11 at 03:19