CakePHP使用相关模型进行“查找”

时间:2013-08-07 22:54:25

标签: php cakephp cakephp-appmodel

我在CakePHP应用程序中使用$ this-> find('threaded')并试图在find(HABTM)中引入相关模型。我尝试了几种方法,比如'join','recursive'和'contains'都没有运气。我正在使用CakePHP 2.3.6

这是我的(工作)代码。

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

    }

}

这会产生以下数组输出


    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
                                    (
                                    )

                            )

                    )

            )

    )

我想要输出的代码是以下


    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
                                (
                                )

                        )

                )

        )

)

我一直在努力工作,这似乎是Cake应该能够轻松处理的事情。在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

对于我想要完成的事情,我采取了一种略微错误的方法。虽然我仍然认为这应该可以从我的EventsController类中实现,但我选择在EventsPromotersController HABTM类中创建它。使用此方法代码更简单


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

    }