Anthony, Thanks a lot! That's what I needed and now I've got it all
figured out :)

On Aug 22, 10:52 am, Anthony <anthony.c.fra...@gmail.com> wrote:
> You'll probably want to use containable.
>
> Check out:http://book.cakephp.org/view/474/Containable -Specifically
> the section on deeper associations.
>
> On Aug 21, 12:41 am, jamie_chong <mr.jamiech...@gmail.com> wrote:
>
> > This is my second day using/learning Cake so please bear with me :)
>
> > I'll try to keep things simple, but there is a lot of data here. I
> > didnt' want to leave anything out.
>
> > Let's say I have these database tables:
>
> > my_lists (id, name)
> > (1, "List One")
> > (2, "List Two")
>
> > items(id, name)
> > (1, "Item One")
> > (2, "Item Two")
> > (3, "Item Three")
> > (4, "Item Four")
>
> > items_my_lists(item_id, my_list_id)
> > (2, 1)
> > (3, 1)
> > (4, 1)
> > (3, 2)
> > (4, 2)
>
> > my_comments(id, my_list_id, item_id, message)
> > (1, 1, NULL, "Comment For List One")
> > (2, NULL, 4, "Comment For Item Four")
> > (3, 1, 4, "Comment for List One, Item Four")
> > (4, 2, 4, "Comment for List Two, Item Four")
>
> > My Models look like this:
>
> > class List extends AppModel
> > {
> >         public $name = "List";
> >         public $hasAndBelongsToMany = "Item";
> >         public $hasMany = "Comment";
>
> > }
>
> > class Item extends AppModel
> > {
> >         public $name = "Item";
> >         public $hasAndBelongsToMany = "List";
> >         public $hasMany = "Comment";
>
> > }
>
> > class Comment extends AppModel
> > {
> >         public $name = "Comment";
>
> > }
>
> > Notice my_comments has Two Foreign Keys (my_list_id, item_id). This is
> > so I can have comments specifically for
> > 1. my_lists (item_id is NULL)
> > 2. items (my_list_id is NULL)
> > 3. items in my_lists (both my_list_id and item_id are set)
>
> > Here is my problem. For each of the two queries (shown below), I only
> > want comments to be returned for Items if and only if they match the
> > item_id AND the list_id. Right now I'm getting Comments that belong to
> > other lists. Furthermore, I don't want comments for Items if either
> > list_id or item_id are NULL. And lastly, comments for MyList should
> > only appear if item_id is NULL.
>
> > Now when I run a find() search in my Controller, I get for "List One":
>
> > $this->MyList->find("first",array(
> >         "conditions"=>array("MyList.id"=>1),
> >         "recursive" => 2
> > ));
>
> > Array
> > (
> >     [MyList] => Array
> >     (
> >         [id] => 1
> >         [name] => List One
> >     )
>
> >     [MyComment] => Array
> >     (
> >         [0] => Array
> >         (
> >             [id] => 1
> >             [my_list_id] => 1
> >             [item_id] =>
> >             [message] => Comment List One
> >         )
>
> >         [1] => Array
> >         (
> >             [id] => 3
> >             [my_list_id] => 1
> >             [item_id] => 4
> >             [message] => Comment List One, Item Four
> >         )
>
> >     )
>
> >     [Item] => Array
> >     (
> >         [0] => Array
> >         (
> >             [id] => 2
> >             [name] => Item Two
> >             [MyComment] => Array
> >             (
> >             )
>
> >         )
>
> >         [1] => Array
> >         (
> >             [id] => 3
> >             [name] => Item Three
> >             [MyComment] => Array
> >             (
> >             )
>
> >         )
>
> >         [2] => Array
> >         (
> >             [id] => 4
> >             [name] => Item Four
> >             [MyComment] => Array
> >             (
> >                 [0] => Array
> >                 (
> >                     [id] => 2
> >                     [my_list_id] =>
> >                     [item_id] => 4
> >                     [message] => Comment Item Four
> >                 )
>
> >                 [1] => Array
> >                 (
> >                     [id] => 3
> >                     [my_list_id] => 1
> >                     [item_id] => 4
> >                     [message] => Comment List One, Item Four
> >                 )
>
> >                 [2] => Array
> >                 (
> >                     [id] => 4
> >                     [my_list_id] => 2
> >                     [item_id] => 4
> >                     [message] => Comment List Two, Item Four
> >                 )
>
> >             )
>
> >         )
>
> >     )
>
> > )
>
> > And this is what I get for List Two
> > $this->MyList->find("first",array(
> >         "conditions"=>array("MyList.id"=>2),
> >         "recursive" => 2
> > ));
>
> > Array
> > (
> >     [MyList] => Array
> >     (
> >         [id] => 2
> >         [name] => List Two
> >     )
>
> >     [MyComment] => Array
> >     (
> >         [0] => Array
> >         (
> >             [id] => 4
> >             [my_list_id] => 2
> >             [item_id] => 4
> >             [message] => Comment List Two, Item Four
> >         )
>
> >     )
>
> >     [Item] => Array
> >     (
> >         [0] => Array
> >         (
> >             [id] => 3
> >             [name] => Item Three
> >             [MyComment] => Array
> >             (
> >             )
>
> >         )
>
> >         [1] => Array
> >         (
> >             [id] => 4
> >             [name] => Item Four
> >             [MyComment] => Array
> >             (
> >                 [0] => Array
> >                 (
> >                     [id] => 2
> >                     [my_list_id] =>
> >                     [item_id] => 4
> >                     [message] => Comment Item Four
> >                 )
>
> >                 [1] => Array
> >                 (
> >                     [id] => 3
> >                     [my_list_id] => 1
> >                     [item_id] => 4
> >                     [message] => Comment List One, Item Four
> >                 )
>
> >                 [2] => Array
> >                 (
> >                     [id] => 4
> >                     [my_list_id] => 2
> >                     [item_id] => 4
> >                     [message] => Comment List Two, Item Four
> >                 )
>
> >             )
>
> >         )
>
> >     )
> > )
>
> > Ideally for the same two queries, I'd get these results:
>
> > $this->MyList->find("first",array(
> >         "conditions"=>array("MyList.id"=>1),
> >         "recursive" => 2
> > ));
>
> > Array
> > (
> >     [MyList] => Array
> >     (
> >         [id] => 1
> >         [name] => List One
> >     )
>
> >     [MyComment] => Array
> >     (
> >         [0] => Array
> >         (
> >             [id] => 1
> >             [my_list_id] => 1
> >             [item_id] =>
> >             [message] => Comment List One
> >         )
> >     )
>
> >     [Item] => Array
> >     (
> >         [0] => Array
> >         (
> >             [id] => 2
> >             [name] => Item Two
> >             [MyComment] => Array
> >             (
> >             )
> >         )
> >         [1] => Array
> >         (
> >             [id] => 3
> >             [name] => Item Three
> >             [MyComment] => Array
> >             (
> >             )
> >         )
> >         [2] => Array
> >         (
> >             [id] => 4
> >             [name] => Item Four
> >             [MyComment] => Array
> >             (
> >                 [0] => Array
> >                 (
> >                     [id] => 3
> >                     [my_list_id] => 1
> >                     [item_id] => 4
> >                     [message] => Comment List One, Item Four
> >                 )
> >             )
> >         )
> >     )
> > )
>
> > $this->MyList->find("first",array(
> >         "conditions"=>array("MyList.id"=>2),
> >         "recursive" => 2
> > ));
>
> > Array
> > (
> >         [MyList] => Array
> >         (
> >                 [id] => 2
> >                 [name] => List Two
> >         )
> >         [MyComment] => Array
> >         (
> >         )
>
> >         [Item] => Array
> >         (
> >                 [0] => Array
> >                 (
> >                         [id] => 3
> >                         [name] => Item Three
> >                         [MyComment] => Array
> >                         (
> >                         )
> >                 )
> >                 [1] => Array
> >                 (
> >                         [id] => 4
> >                         [name] => Item Four
> >                         [MyComment] => Array
> >                         (
> >                             [0] => Array
> >                             (
> >                                 [id] => 4
> >                                 [my_list_id] => 2
> >                                 [item_id] => 4
> >                                 [message] => Comment List Two, Item Four
> >                             )
>
> >                         )
>
> >                 )
>
> >         )
> > )

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to