Friday, April 26, 2013

Re: Error on find on hasAndBelongsToMany relationed models

Dear Ciul,

As Reuben said  HABTM generally doesn't do a join, but if you want to joins then you will need to set up an extra table in the database to handle this relationship.


Cheers!..

Saran


On Thu, Apr 25, 2013 at 5:49 PM, Reuben <reuben.helms@gmail.com> wrote:
The HABTM generally doesn't do a join.  You would only use a straight find when you have conditions for only one of the models.  

Alternatives are to use contains, or use join to explicitly define the join and the join conditions.  Whilst contains is handy, I usually end up going with the join clause, because it matches mostly closely with what I'd do, if I was writing straight SQL.

Regards
Reuben Helms


On Thursday, 25 April 2013 02:47:31 UTC+10, Ciul wrote:
I'm having troubles to restrict a find operation based on a related model field within a hasAndBelongsToMany relationship.

// Wedding Model
public $hasAndBelongsToMany = array('Guest');

// Guest Model
public $hasAndBelongsToMany = array('Wedding');

Then, calling in Guest Controller:

// GuestsController Controller
$this->Guest->find('first', array(
      'conditions' => array(
            'Guest.email' => 'te...@test.com',
            'Wedding.alias' => 'maria-wedding'
      ),
      'recursive' => 1
);


ERROR:

Column not found: 1054 Unknown column 'Wedding.alias' in 'where clause'

SQL Query: SELECT `Guest`.`id`, `Guest`.`created`, `Guest`.`modified`, `Guest`.`name`, `Guest`.`email`, `Guest`.`enabled`, `Guest`.`confirmed`

FROM `weddingshare`.`demo_guests` AS `Guest` WHERE `Guest`.`email` = 'te...@test.com' AND `Wedding`.`alias` = 'maria-wedding' LIMIT 1


I understand it is not event joining tables to make the request.

Please, I'd appreciate any help.

Regards

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: