开发者

CakePHP hasMany - belongsTo - hasMany relationship

I have a bit more complex system... I have

  • u开发者_开发百科sers model - hasMany Ads
  • ads model - hasMany "placads"
  • placads model - belongsTo Ads and Places
  • places model - hasMany placads

Now I run query like this:

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

and I am getting this without "Place" in * mark

[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
                        )*
                    )

            )

    )

What should I do to get something like I wrote into the * mark?

Thanks in advance!


use recursive:

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

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

alternatively:

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

number 2 is an example, but should do in your case

2nd alternative:

use Containable

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜