与顶级模型有关的深层关联?

时间:2012-03-22 03:56:05

标签: cakephp

我有以下型号:

  • Kin,您可以将其视为动物 - 宠物。
  • Species hasAndBelongsToMany Kin
  • GrowthStage hasAndBelongsToMany Species
  • Kin hasAndBelongsToMany GrowthStage
  • KinGrowthStage,它定义了亲属及其成长阶段之间的关系(它包含了相关图像以及在该成长阶段的亲属的相关图像)。

所以在我的控制器中,我试图让以下数组从另一端出来。在这种情况下,Species => GrowthStage => KinGrowthStages与Kin id相关:

Array
(
    [Kin] => Array
    (
        [id] => 4
        [name] => Hobbes
        [generation] => 1
        [current_growth_stage_id] => 4
        [species_id] => 1
    )
    [Species] => Array
    (
        [id] => 1
        [name] => Tiger
        [GrowthStage] => Array
        (
            [0] => Array
            (
                [id] => 5
                [name] => cub
                [order] => 1
                [KinGrowthStage] => Array
                (
                    [0] => Array
                    (
                        [id] => 1
                        [growth_stage_id] => 1
                        [kin_id] => 4
                        [image] => /img/hobbes/cub.png
                    )
                )
            )
            [1] => Array
            (
                [id] => 2
                [name] => juvenile
                [order] => 2
                [KinGrowthStage] => Array
                (
                    [0] => Array
                    (
                        [id] => 1
                        [growth_stage_id] => 1
                        [kin_id] => 4
                        [image] => /img/hobbes/juvenile.png
                    )
                )
            )
            [2] => Array
            (
                [id] => 9
                [name] => adult
                [order] => 3
                [KinGrowthStage] => Array
                (
                    // Nothing here because Hobbes hasn't reached this growth stage yet
                )
            )
        )
    )
)

我得到的是这个。物种=> GrowthStage => KinGrowthStages正在加入GrowthStage id而不是Kin id:

Array
(
    [Kin] => Array
    (
        [id] => 4
        [name] => Hobbes
        [generation] => 1
        [current_growth_stage_id] => 4
        [species_id] => 1
    )
    [Species] => Array
    (
        [id] => 1
        [name] => Tiger
        [GrowthStage] => Array
        (
            [0] => Array
            (
                [id] => 5
                [name] => cub
                [order] => 1
                [KinGrowthStage] => Array
                (
                    [0] => Array
                    (
                        [id] => 1
                        [growth_stage_id] => 1
                        [kin_id] => 1
                        [image] => /img/tony/cub.png
                    )
                )
            )
            [1] => Array
            (
                [id] => 2
                [name] => juvenile
                [order] => 2
                [KinGrowthStage] => Array
                (
                    [0] => Array
                    (
                        [id] => 1
                        [growth_stage_id] => 2
                        [kin_id] => 1
                        [image] => /img/tony/juvenile.png
                    )
                )
            )
            [2] => Array
            (
                [id] => 9
                [name] => adult
                [order] => 3
                [KinGrowthStage] => Array
                (
                    [0] => Array
                    (
                        [id] => 1
                        [growth_stage_id] => 3
                        [kin_id] => 1
                        [image] => /img/tony/adult.png
                    )
                )
            )
        )
    )
)

我理解为什么发生了这种情况,我只是想知道是否有任何方法可以“动态”修改关系并将其加入到亲属身份上。我已经尝试将以下条件数组添加到我的包含数组中:

$this->Kin->contain(
    array(
        'Species' => array(
            'GrowthStage' => array(
                'KinGrowthStage' => array(
                    'conditions' => array('Kin.id = KinGrowthStage.kin_id'),
                ),
                'order' => 'GrowthStage.order ASC'
            )
        )
    )
);

但我收到有关'未知表Kin'的错误。

1 个答案:

答案 0 :(得分:0)

使用可链接行为https://github.com/Terr/linkable

$this->Kin->find('all', array(
    'link'  => array(
        'Species'  => array(
            'GrowthStage' => array(
                'KinGrowthStage' => array(
                    'conditions'    => 'Kin.id = KinGrowthStage.kin_id', 
                )
            )
        )
    ),
    'order' => 'GrowthStage.order ASC'
));