2
votes

I am using a $this->find('threaded') in my CakePHP application and am trying to pull in an associated model within the find (HABTM). I have tried several methods, such as 'joins', 'recursive' and 'contains' all with no luck. I am using CakePHP 2.3.6

Here is my (working) code.

class EventsController extends AppController {

    public function promoters($id = null) {

     $options = array('conditions' => array('Event.id' => $id));
     $event = $this->Event->find('first', $options);
     $this->set('event', $event);

     $this->loadModel('EventsPromoter');
     $treelistConditions = array(
            'conditions' => array(
                'event_id' => $id
            ),
     );
     $promoterTree = $this->EventsPromoter->find('threaded', $treelistConditions);
     $this->set('promoters', $promoterTree);

    }

}

This results in the following array output


    Array
    (
        [0] => Array
            (
                [EventsPromoter] => Array
                    (
                        [id] => 1
                        [promoter_id] => 1
                        [event_id] => 1
                        [parent_id] => 
                        [created] => 2013-07-26 00:30:09
                        [modified] => 2013-07-26 00:30:09
                    )

                [children] => Array
                    (
                        [0] => Array
                            (
                                [EventsPromoter] => Array
                                    (
                                        [id] => 10
                                        [promoter_id] => 4
                                        [event_id] => 1
                                        [parent_id] => 1
                                        [created] => 0000-00-00 00:00:00
                                        [modified] => 0000-00-00 00:00:00
                                    )

                                [children] => Array
                                    (
                                    )

                            )

                        [1] => Array
                            (
                                [EventsPromoter] => Array
                                    (
                                        [id] => 13
                                        [promoter_id] => 6
                                        [event_id] => 1
                                        [parent_id] => 1
                                        [created] => 0000-00-00 00:00:00
                                        [modified] => 0000-00-00 00:00:00
                                    )

                                [children] => Array
                                    (
                                    )

                            )

                    )

            )

        [1] => Array
            (
                [EventsPromoter] => Array
                    (
                        [id] => 2
                        [promoter_id] => 2
                        [event_id] => 1
                        [parent_id] => 
                        [created] => 2013-07-26 00:30:09
                        [modified] => 2013-07-26 00:30:09
                    )

                [children] => Array
                    (
                        [0] => Array
                            (
                                [EventsPromoter] => Array
                                    (
                                        [id] => 11
                                        [promoter_id] => 5
                                        [event_id] => 1
                                        [parent_id] => 2
                                        [created] => 0000-00-00 00:00:00
                                        [modified] => 0000-00-00 00:00:00
                                    )

                                [children] => Array
                                    (
                                        [0] => Array
                                            (
                                                [EventsPromoter] => Array
                                                    (
                                                        [id] => 6
                                                        [promoter_id] => 3
                                                        [event_id] => 1
                                                        [parent_id] => 11
                                                        [created] => 0000-00-00 00:00:00
                                                        [modified] => 0000-00-00 00:00:00
                                                    )

                                                [children] => Array
                                                    (
                                                    )

                                            )

                                    )

                            )

                        [1] => Array
                            (
                                [EventsPromoter] => Array
                                    (
                                        [id] => 14
                                        [promoter_id] => 7
                                        [event_id] => 1
                                        [parent_id] => 2
                                        [created] => 2013-07-26 00:30:09
                                        [modified] => 2013-07-26 00:30:09
                                    )

                                [children] => Array
                                    (
                                    )

                            )

                    )

            )

    )

What I would like my code to output is the following


    Array
    (
        [0] => Array
            (
                [EventsPromoter] => Array
                    (
                        [id] => 1
                        [promoter_id] => 1
                        [event_id] => 1
                        [parent_id] => 
                        [created] => 2013-07-26 00:30:09
                        [modified] => 2013-07-26 00:30:09
                    )

                [Promoter] => Array
                    (
                        [promoter_id] => 1
                        [first_name] => 'Bob'
                        [last_name] => 'Smith'
                    )

                [children] => Array
                    (
                        [0] => Array
                            (
                                [EventsPromoter] => Array
                                    (
                                        [id] => 10
                                        [promoter_id] => 4
                                        [event_id] => 1
                                        [parent_id] => 1
                                        [created] => 0000-00-00 00:00:00
                                        [modified] => 0000-00-00 00:00:00
                                    )
                                [Promoter] => Array
                                    (
                                        [promoter_id] => 4
                                        [first_name] => 'Sally'
                                        [last_name] => 'Sue'
                                    )

                                [children] => Array
                                    (
                                    )

                            )

                        [1] => Array
                            (
                                [EventsPromoter] => Array
                                    (
                                        [id] => 13
                                        [promoter_id] => 6
                                        [event_id] => 1
                                        [parent_id] => 1
                                        [created] => 0000-00-00 00:00:00
                                        [modified] => 0000-00-00 00:00:00
                                    )

                                [Promoter] => Array
                                    (
                                        [promoter_id] => 6
                                        [first_name] => 'Ben'
                                        [last_name] => 'King'
                                    )

                                [children] => Array
                                    (
                                    )

                            )

                    )

            )

        [1] => Array
            (
                [EventsPromoter] => Array
                    (
                        [id] => 2
                        [promoter_id] => 2
                        [event_id] => 1
                        [parent_id] => 
                        [created] => 2013-07-26 00:30:09
                        [modified] => 2013-07-26 00:30:09
                    )

                [Promoter] => Array
                    (
                        [promoter_id] => 2
                        [first_name] => 'Jack'
                        [last_name] => 'Sparrow'
                    )

                [children] => Array
                    (
                        [0] => Array
                            (
                                [EventsPromoter] => Array
                                    (
                                        [id] => 11
                                        [promoter_id] => 5
                                        [event_id] => 1
                                        [parent_id] => 2
                                        [created] => 0000-00-00 00:00:00
                                        [modified] => 0000-00-00 00:00:00
                                    )

                                [Promoter] => Array
                                    (
                                        [promoter_id] => 5
                                        [first_name] => 'Jane'
                                        [last_name] => 'Doe'
                                    )

                                [children] => Array
                                    (
                                        [0] => Array


                                      (
                                            [EventsPromoter] => Array
                                                (
                                                    [id] => 6
                                                    [promoter_id] => 3
                                                    [event_id] => 1
                                                    [parent_id] => 11
                                                    [created] => 0000-00-00 00:00:00
                                                    [modified] => 0000-00-00 00:00:00
                                                )

                                            [Promoter] => Array
                                                (
                                                    [promoter_id] => 3
                                                    [first_name] => 'Mike'
                                                    [last_name] => 'Jones'
                                                )

                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [EventsPromoter] => Array
                                (
                                    [id] => 14
                                    [promoter_id] => 7
                                    [event_id] => 1
                                    [parent_id] => 2
                                    [created] => 2013-07-26 00:30:09
                                    [modified] => 2013-07-26 00:30:09
                                )

                            [Promoter] => Array
                                (
                                    [promoter_id] => 7
                                    [first_name] => 'Spider'
                                    [last_name] => 'Man'
                                )

                            [children] => Array
                                (
                                )

                        )

                )

        )

)

Nothing I have been trying is working, and this seems like something Cake should be able to handle fairly easily. Thanks in advance for any help!

1
Did you try 'contain' => array('Promoter') (note the missing s - not contains)? Also: You should always mention the exact cakephp version you are using.mark
Yes, I tried that also, here is what that code looked like: $treelistConditions = array( 'conditions' => array( 'event_id' => $id ), 'contain' => array('Promoter'), ); Also, I updated the question with the correct version of CakePHPBryan Zwicker

1 Answers

1
votes

I was taking a slightly wrong approach for what I was trying to accomplish. While I still think this should be possible from my EventsController class, I elected to make it in the EventsPromotersController HABTM class. The code is much simpler using this method


    class EventsPromotersController extends AppController {

        public function event($event_id = null) {
                $options = array('conditions' => array('event_id' => $event_id));
                $promoters = $this->EventsPromoter->find('threaded', $options);
                $this->set('promoters', $promoters);
        }

    }