Nope, that doesn't change anything at all....

The problem is with the way the condition gets used.

In the example above, where Lessons belongsTo Parts, calling something
such as

$this->Lesson->findAll(array('Part.id' => 1))  will, in fact, return
all lessons attributed to a part with that id.

but when trying to do the same thing where Lessons HABTM Pieces,
findAll returns the error...

SQL Error: 1054: Unknown column 'Piece.id' in 'where clause'

I looked at the SQL commands being generated (debug = 2) and I think I
found the problem (although still no easy solution)

The WHERE clause (WHERE `Piece`.`id` = 1)  is called before the SQL
SELECT command is called which retrieves the pieces table and joins it
to the lessons table.

How to get the findAll or any other simple command to find all lessons
associated with a certain piece is still beyond me.  This shouldn't be
that hard of a thing to do.  It should be akin to searching for all
posts associated to a tag, a very common thing.

On Oct 25, 2:55 pm, dardosordi <[EMAIL PROTECTED]> wrote:
> Because you are missing the $this->Lesson->recursive = 1;
>
> On Oct 25, 3:25 pm, EVan <[EMAIL PROTECTED]> wrote:
>
>
>
> > No, the idea is to search Lessons and find lessons with a specific
> > piece.
>
> > In other words....
>
> > $resultArray = $this->Lesson->findAll(array('Piece.id' => '1'));
>
> > This doesn't work though.
>
> > On Oct 25, 1:58 pm, rtconner <[EMAIL PROTECTED]> wrote:
>
> > > You want to search Pieces and list the lessons related to those
> > > pieces?
>
> > > $this->Lesson->Piece->recursive = 1;
> > > $this->Lesson->Piece->findAll(array("Piece.name LIKE '%asd%'"));
>
> > > or to just get all lessons and thier related pieces
>
> > > $this->Lesson->recursive = 1;
> > > $this->Lesson->findAll();
>
> > > On Oct 25, 11:47 am, EVan <[EMAIL PROTECTED]> wrote:
>
> > > > 4 hours later, still can't figure this out.....
>
> > > > I've found that...
>
> > > > $this->Lesson->Piece->findAll(array('Piece.id'=>'1'));
>
> > > > will return some Lesson info...
>
> > > > but I was under the impression
>
> > > > $this->Lesson->Piece->findAll() was incorporated in
>
> > > > $this->Lesson->findAll()
>
> > > > ?
>
> > > > On Oct 25, 12:21 pm, EVan <[EMAIL PROTECTED]> wrote:
>
> > > > > I'm on day two of trying to figure this out, we'll see if this helps.
>
> > > > > I've got three Models.  Lessons belongsTo Parts and Lessons HABTM
> > > > > Pieces (and vice-versa)
>
> > > > > Here's the model definition for Lessons, pretty straightforward.
>
> > > > > class Lesson extends AppModel {
> > > > >         var $name = 'Lesson';
> > > > >         var $belongsTo = array(
> > > > >                 'Part' => array(
> > > > >                         'className' => 'Part',
> > > > >                         'foreignKey' => 'part_id'
> > > > >                 )
> > > > >         );
>
> > > > >         var $hasAndBelongsToMany = array(
> > > > >                 'Piece' => array(
> > > > >                         'className' => 'Piece',
> > > > >                         'joinTable' => 'lessons_pieces',
> > > > >                         'foreignKey' => 'lesson_id',
> > > > >                         'associationForeignKey'=> 'piece_id',
> > > > >                         'unique' => true
> > > > >                 )
> > > > >         );
>
> > > > > }
>
> > > > > The problem comes when trying to use findAll to locate a lesson
> > > > > associated with a particular piece.  This is the findAll command I'm
> > > > > using with the conditions array....
>
> > > > > $resultArray = $this->Lesson->findAll(array('Piece.id' => '1',
> > > > > 'Lesson.part_id' => '2'));
>
> > > > > Looking at the SELECT query generated by Cake, I see that, unlike the
> > > > > parts table, the pieces table is essntially ignored by findAll even
> > > > > though there's a HABTM association, thus the WHERE part of the query,
> > > > > where I ask for Piece.id, generates an error.
>
> > > > > Query: SELECT `Lesson`.`id`, `Lesson`.`created`, `Lesson`.`modified`,
> > > > > `Lesson`.`title`, `Lesson`.`issue`, `Lesson`.`part_id`, `Part`.`id`,
> > > > > `Part`.`part` FROM `lessons` AS `Lesson` LEFT JOIN `parts` AS `Part`
> > > > > ON (`Lesson`.`part_id` = `Part`.`id`) WHERE `Piece`.`id` = 1 AND
> > > > > `Lesson`.`part_id` = 2
>
> > > > > I'm confused as to how I might search for HABTM model associations via
> > > > > a simple method such as findAll...  I feel this might be related to
> > > > > the following --->  https://trac.cakephp.org/ticket/1209-Hidequoted 
> > > > > text -
>
> > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to