开发者

How to get HABTM associated data using hasOne binding

I am following example documented at http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM

I am trying to retrieve associated data using hasOne.

I created 3 tables posts, tags and posts_tags. I wrote following code to debug Posts.

$this->Post->bindModel(array(
    'hasOne' => array(
        'PostsTag',
        'FilterTag' => array(
            'className' => 'Tag',
            'foreignKey' => false,
            'conditions' => array('FilterTag.id = PostsTag.tag_id')
))));
$output=$this->Post->find('all', array(
        'fields' => array('Post.*')
));
debug($output);

I was expecting output something like below.

Array
(  
    0 => Array
        {
        [Post] => Array
            (
                [id] => 1
                [title] => test post 1
            )
    [Tag] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => php
                )
           [1] => Array
                (
                    [id] => 2
                    [name] => javascript
                )
           [2] => Array
                (
                    [id] => 3
                    [name] => xml
                )
        )
}

But my output do not have Tags at all. Here is what I got.

Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    [id] => 1
                    [title] => test post1
                )

        )

    [1] => Array
        (
            [Post] => Array
                (
                    [id] => 2
                    [title] => test post2
                )

        )
)

How do I get associated tags along with the post.

I know I am missing something, but unable to figure out. Any help would be highly appreciated.


Edit 1:

Ok 开发者_开发百科I tried few more variants.

I tried:

$this->Post->bindModel(array(
'hasOne' => array(
        'PostsTag',
        'FilterTag' => array(
            'className' => 'Tag',
            'foreignKey' => false,
            'conditions' => array('FilterTag.id = PostsTag.tag_id')
))));
$output=$this->Post->find('all');

I got:

Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    [id] => 1
                    [title] => test post1
                )

            [PostsTag] => Array
                (
                    [id] => 1
                    [post_id] => 1
                    [tag_id] => 1
                )

            [FilterTag] => Array
                (
                    [id] => 1
                    [name] => php
                )

        )

    [1] => Array
        (
            [Post] => Array
                (
                    [id] => 1
                    [title] => test post1
                )

            [PostsTag] => Array
                (
                    [id] => 2
                    [post_id] => 1
                    [tag_id] => 2
                )

            [FilterTag] => Array
                (
                    [id] => 2
                    [name] => javascript
                )

        )

    [2] => Array
        (
            [Post] => Array
                (
                    [id] => 1
                    [title] => test post1
                )

            [PostsTag] => Array
                (
                    [id] => 3
                    [post_id] => 1
                    [tag_id] => 3
                )

            [FilterTag] => Array
                (
                    [id] => 3
                    [name] => xml
                )

        )
)

I tried:

$output=$this->Post->find('all', array(
        'fields' => array('Post.*', 'FilterTag.*'),
        'recursive' => 1
));

I got:

Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    [id] => 1
                    [title] => test post1
                )

            [FilterTag] => Array
                (
                    [id] => 1
                    [name] => php
                )

        )

    [1] => Array
        (
            [Post] => Array
                (
                    [id] => 1
                    [title] => test post1
                )

            [FilterTag] => Array
                (
                    [id] => 2
                    [name] => javascript
                )

        )

    [2] => Array
        (
            [Post] => Array
                (
                    [id] => 1
                    [title] => test post1
                )

            [FilterTag] => Array
                (
                    [id] => 3
                    [name] => xml
                )

        )
)

Just in case I am missing something, here is my Posts controller:

class PostsController extends AppController {
    var $name = 'Posts';
    var $helpers = array('Html','Ajax','Javascript');
    var $components = array( 'RequestHandler' );

    function index() {
        $this->Post->bindModel(array(
            'hasOne' => array(
                'PostsTag',
                'FilterTag' => array(
                    'className' => 'Tag',
                    'foreignKey' => false,
                    'conditions' => array('FilterTag.id = PostsTag.tag_id')
        ))));

        $output=$this->Post->find('all', array(
                'fields' => array('Post.*', 'FilterTag.*'),
                'recursive' => 1
        ));

    }
}

And here is my Posts model:

class Post extends AppModel {
    var $name = 'Post';
}

I still wonder why is the example from cookbook not working.


I would recommend checking 3 things:

  1. See if you have set your associations right (hasMany, hasAndBelongsToMany)

    Make sure you have set the Post HABTM Tag and Tag HABTM Post relationships correctly.

  2. Check the value of $recursive

    Most of the examples in the CakePHP book require that you have your $recursive property set to 1.

  3. Remove the fields restriction: 'fields' => array('Post.*')

    It's possible that Cake is restricting only the Post fields, which is why no Tag fields are showing up. Remove this restriction and try it again.

Let us know if this works for you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜