Thursday, January 29, 2009

Re: Find with two levels of recursion in where clause

Thanks a million that was exactly what i needed.

Heres an example of the code that does what you instructed that may
help someone else in the future, if it can be improved please let me
know.

The code executes from within the A model class (A.php)

$modelB = ClassRegistry::init('B');
$modelB->unbindModel(array('belongsTo' => array('C)));
$this->bindModel(array('belongsTo' => array('C' => array(
'className' => 'C',
'foreignKey' => false,
'conditions' => 'B.c_id = C.id',
))));
$conditions = array('C.somefield' => 'somevalue');
$this->find('all', array('conditions' => $conditions));


On Jan 29, 1:31 am, WebbedIT <p...@webbedit.co.uk> wrote:
> You need to force a join from A to C using B so that you can specify
> conditions on C fields within your find ... sounds easy when I write
> it like that, and in fact it is quite easy once you get your head
> around it.
>
> You need to unbind the relationship between B and C then bind A to C
> setting 'foreignKey'=>false, and supplying your own conditions array
> with 'B.c_id = c.id'.  Tbis works because A is associated with B so
> you can use fields of model B in the condition when joining A to C but
> you need to make sure B is not associated with C at the time hence the
> need to unbind them first.
>
> This information comes courtesy of a few articles written by teknoid
> so give them a read at:http://teknoid.wordpress.com/?s=force+join
>
> Paul.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: