简体   繁体   中英

CakePHP Threaded “Find” with Associated Models

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!

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);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM