Wednesday, October 29, 2008

Re: hasMany bind on the fly

So it just can't handle the conditions?
What should I do in this case? create my own query?

On Oct 29, 7:29 pm, teknoid <teknoid.cake...@gmail.com> wrote:
> cake does not create joins for hasMany (only hasOne and belongsTo)
>
> On Oct 29, 12:27 pm, Ita <zip...@gmail.com> wrote:
>
> > Hi,
> > I have 2 models Item and Defect.
> > both of them have description.
> > an Item can have many Defects and my defects table has a an item_id
> > field.
>
> > Now I'm trying to create a search query that will search both the item
> > description and the defect description.
> > What I do is this:
>
> > $this->Item->bindModel(
> >                             array('hasMany' => array(
> >                                 'Defect' => array(
> >                                     'className' => 'Defect',
> >                                     'foreignKey' => 'item_id',
> >                                     'type' => 'INNER'
> >                                     )
> >                                 )
> >                             )
> >                     );
> > then I run findAll.
>
> > Now when I use findAll with no conditions everything works fine. cake
> > seems to run 2 queries:
> > SELECT `Item`.`id`,...... FROM `items` AS `Item` WHERE 1 = 1
> > SELECT `Defect`.`id`,.......... FROM `defects` AS `Defect` WHERE
> > `Defect`.`item_id` IN (1, 2, 16, 15, 14, 13, 12, 11, 10, 17, 18, 19,
> > 20)
>
> > My first question is why does cake doesn't create one query with INNER
> > JOIN?
> > The second problem is when I add conditions:
> > $conditions[] = "`Item`.`name` LIKE '%" .$search_term ."%' OR
> > `Defect`.`description` LIKE '%" . $search_term . "%'";
>
> > It seems that cake in this case ignores the join all together and just
> > does a regular select so I get an error:
> > 1054: Unknown column 'Defect.description' in 'where clause'
>
> > Does anyone know what I'm doing wrong?
--~--~---------~--~----~------------~-------~--~----~
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: