CakePHP hasMany - belongsTo - hasMany关系

时间:2011-08-18 12:09:00

标签: cakephp

我的系统有点复杂...... 我有

  • 用户模型 - hasMany广告
  • 广告模型 - hasMany“placads”
  • placads model - belongsTo Ads and Places
  • 放置模型 - hasMany placads

现在我运行这样的查询:

    $this->set(
        'ad',
        $this->User->Ad->find(
            'all',
            array(
                'conditions' => array('Ad.user_id' => $this->Auth->user('id'))
            )
        )
    );

我在* mark

中没有“Place”
[0] => Array
    (
        [Ad] => Array
            (
                [id] => 1
                [user_id] => 1
                [name] => bota
            )

        [Placad] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [ad_id] => 1
                        [place_id] => 1
                        [count] => 10
                        *[Place] => Array
                        (
                            info about place
                        )*
                    )

                [1] => Array
                    (
                        [id] => 2
                        [ad_id] => 1
                        [place_id] => 2
                        [count] => 20
                        *[Place] => Array
                        (
                            info about place
                        )*
                    )

            )

    )

我该怎样做才能得到像我写入*标记的内容?

提前致谢!

1 个答案:

答案 0 :(得分:4)

使用递归:

http://book.cakephp.org/view/1063/recursive

$this->User->Ad->recursive = 2; // before find

可替换地:

$this->User->Ad->find( 'all', 
    array(
        'recursive' => 2,
        'conditions' => array('Ad.user_id' => $this->Auth->user('id'))
    )
)

数字2是一个例子,但在你的情况下应该这样做

第二种选择:

使用Containable

http://book.cakephp.org/view/1323/Containable