2

我在我的 CakePHP 应用程序中使用 $this->find('threaded') 并试图在 find (HABTM) 中拉入关联模型。我尝试了几种方法,例如“joins”、“recursive”和“contains”,但都没有成功。我正在使用 CakePHP 2.3.6

这是我的(工作)代码。

类 EventsController 扩展 AppController {

    公共功能发起人($id = null){

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

     $this->loadModel('EventsPromoter');
     $treelistConditions = 数组(
            '条件' => 数组(
                'event_id' => $id
            ),
     );
     $promoterTree = $this->EventsPromoter->find('thread', $treelistConditions);
     $this->set('promoters', $promoterTree);

    }

}

这导致以下数组输出

    大批
    (
        [0] => 数组
            (
                [EventsPromoter] => 数组
                    (
                        [id] => 1
                        [promoter_id] => 1
                        [event_id] => 1
                        [parent_id] =>
                        [创建] => 2013-07-26 00:30:09
                        [修改] => 2013-07-26 00:30:09
                    )

                [孩子] => 数组
                    (
                        [0] => 数组
                            (
                                [EventsPromoter] => 数组
                                    (
                                        [id] => 10
                                        [promoter_id] => 4
                                        [event_id] => 1
                                        [parent_id] => 1
                                        [创建] => 0000-00-00 00:00:00
                                        [修改] => 0000-00-00 00:00:00
                                    )

                                [孩子] => 数组
                                    (
                                    )

                            )

                        [1] => 数组
                            (
                                [EventsPromoter] => 数组
                                    (
                                        [id] => 13
                                        [promoter_id] => 6
                                        [event_id] => 1
                                        [parent_id] => 1
                                        [创建] => 0000-00-00 00:00:00
                                        [修改] => 0000-00-00 00:00:00
                                    )

                                [孩子] => 数组
                                    (
                                    )

                            )

                    )

            )

        [1] => 数组
            (
                [EventsPromoter] => 数组
                    (
                        [id] => 2
                        [promoter_id] => 2
                        [event_id] => 1
                        [parent_id] =>
                        [创建] => 2013-07-26 00:30:09
                        [修改] => 2013-07-26 00:30:09
                    )

                [孩子] => 数组
                    (
                        [0] => 数组
                            (
                                [EventsPromoter] => 数组
                                    (
                                        [id] => 11
                                        [promoter_id] => 5
                                        [event_id] => 1
                                        [parent_id] => 2
                                        [创建] => 0000-00-00 00:00:00
                                        [修改] => 0000-00-00 00:00:00
                                    )

                                [孩子] => 数组
                                    (
                                        [0] => 数组
                                            (
                                                [EventsPromoter] => 数组
                                                    (
                                                        [id] => 6
                                                        [promoter_id] => 3
                                                        [event_id] => 1
                                                        [parent_id] => 11
                                                        [创建] => 0000-00-00 00:00:00
                                                        [修改] => 0000-00-00 00:00:00
                                                    )

                                                [孩子] => 数组
                                                    (
                                                    )

                                            )

                                    )

                            )

                        [1] => 数组
                            (
                                [EventsPromoter] => 数组
                                    (
                                        [id] => 14
                                        [promoter_id] => 7
                                        [event_id] => 1
                                        [parent_id] => 2
                                        [创建] => 2013-07-26 00:30:09
                                        [修改] => 2013-07-26 00:30:09
                                    )

                                [孩子] => 数组
                                    (
                                    )

                            )

                    )

            )

    )

我希望我的代码输出如下

    大批
    (
        [0] => 数组
            (
                [EventsPromoter] => 数组
                    (
                        [id] => 1
                        [promoter_id] => 1
                        [event_id] => 1
                        [parent_id] =>
                        [创建] => 2013-07-26 00:30:09
                        [修改] => 2013-07-26 00:30:09
                    )

                [发起人] => 数组
                    (
                        [promoter_id] => 1
                        [first_name] => '鲍勃'
                        [姓氏] => '史密斯'
                    )

                [孩子] => 数组
                    (
                        [0] => 数组
                            (
                                [EventsPromoter] => 数组
                                    (
                                        [id] => 10
                                        [promoter_id] => 4
                                        [event_id] => 1
                                        [parent_id] => 1
                                        [创建] => 0000-00-00 00:00:00
                                        [修改] => 0000-00-00 00:00:00
                                    )
                                [发起人] => 数组
                                    (
                                        [promoter_id] => 4
                                        [first_name] => '莎莉'
                                        [姓氏] => '苏'
                                    )

                                [孩子] => 数组
                                    (
                                    )

                            )

                        [1] => 数组
                            (
                                [EventsPromoter] => 数组
                                    (
                                        [id] => 13
                                        [promoter_id] => 6
                                        [event_id] => 1
                                        [parent_id] => 1
                                        [创建] => 0000-00-00 00:00:00
                                        [修改] => 0000-00-00 00:00:00
                                    )

                                [发起人] => 数组
                                    (
                                        [promoter_id] => 6
                                        [first_name] => '本'
                                        [姓氏] => '国王'
                                    )

                                [孩子] => 数组
                                    (
                                    )

                            )

                    )

            )

        [1] => 数组
            (
                [EventsPromoter] => 数组
                    (
                        [id] => 2
                        [promoter_id] => 2
                        [event_id] => 1
                        [parent_id] =>
                        [创建] => 2013-07-26 00:30:09
                        [修改] => 2013-07-26 00:30:09
                    )

                [发起人] => 数组
                    (
                        [promoter_id] => 2
                        [first_name] => '杰克'
                        [姓氏] => '麻雀'
                    )

                [孩子] => 数组
                    (
                        [0] => 数组
                            (
                                [EventsPromoter] => 数组
                                    (
                                        [id] => 11
                                        [promoter_id] => 5
                                        [event_id] => 1
                                        [parent_id] => 2
                                        [创建] => 0000-00-00 00:00:00
                                        [修改] => 0000-00-00 00:00:00
                                    )

                                [发起人] => 数组
                                    (
                                        [promoter_id] => 5
                                        [first_name] => '简'
                                        [姓氏] => 'Doe'
                                    )

                                [孩子] => 数组
                                    (
                                        [0] => 数组


                                      (
                                            [EventsPromoter] => 数组
                                                (
                                                    [id] => 6
                                                    [promoter_id] => 3
                                                    [event_id] => 1
                                                    [parent_id] => 11
                                                    [创建] => 0000-00-00 00:00:00
                                                    [修改] => 0000-00-00 00:00:00
                                                )

                                            [发起人] => 数组
                                                (
                                                    [promoter_id] => 3
                                                    [first_name] => '迈克'
                                                    [姓氏] => '琼斯'
                                                )

                                            [孩子] => 数组
                                                (
                                                )

                                        )

                                )

                        )

                    [1] => 数组
                        (
                            [EventsPromoter] => 数组
                                (
                                    [id] => 14
                                    [promoter_id] => 7
                                    [event_id] => 1
                                    [parent_id] => 2
                                    [创建] => 2013-07-26 00:30:09
                                    [修改] => 2013-07-26 00:30:09
                                )

                            [发起人] => 数组
                                (
                                    [promoter_id] => 7
                                    [first_name] => '蜘蛛'
                                    [姓氏] => '男人'
                                )

                            [孩子] => 数组
                                (
                                )

                        )

                )

        )

)

我一直在尝试的任何事情都不起作用,这似乎是 Cake 应该能够相当容易地处理的事情。提前感谢您的帮助!

4

1 回答 1

1

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

    类 EventsPromotersController 扩展 AppController {

        公共函数事件($event_id = null){
                $options = array('conditions' => array('event_id' => $event_id));
                $promoters = $this->EventsPromoter->find('thread', $options);
                $this->set('promoters', $promoters);
        }

    }

于 2013-08-08T14:32:00.870 回答