cakephp paginate multiple habtm

Posted by izmanromli on Stack Overflow See other posts from Stack Overflow or by izmanromli
Published on 2010-03-14T23:10:57Z Indexed on 2010/03/14 23:15 UTC
Read the original article Hit count: 341

Filed under:
|

hi guys,

i have multiple habtm like these :

// User model
var $hasMany = array('Post');

// Post model
var $hasAndBelongsToMany = array('Category', 'Tag');

// Category model
var $hasAndBelongsToMany = array('Post');

// Tag model
var $hasAndBelongsToMany = array('Post');

I tried to fetch all post along with its user and tags (within a certain category), somehow if i fetch tags, the result was wrong.

$this->paginate = array
(
    'Post' => array
    (
        'limit' => 2,

        'fields' => array(
            'Post.title', 'Post.content', 'Post.slug', 'Post.created',
            'Tag.name',
            'User.username', 'User.created', 'User.post_count', 'User.avatar_file_name'),

        'joins' => array
        (
            array(
                'table' => 'categories_posts',
                'alias' => 'CategoriesPost',
                'type' => 'inner',
                'conditions'=> array('CategoriesPost.post_id = Post.id')
            ),

            // FETCH USER
            array(
                'table' => 'users',
                'alias' => 'User',
                'type' => 'inner',
                'conditions'=> array('Post.user_id = User.id')
            ),

            // FETCH TAGS
            array(
                'table' => 'posts_tags',
                'alias' => 'PostsTag',
                'type' => 'inner',
                'conditions'=> array('PostsTag.post_id = Post.id')
            ),

            array(
                'table' => 'tags',
                'alias' => 'Tag',
                'type' => 'inner',
                'conditions'=> array('Tag.id = PostsTag.tag_id')
            ),

            array(
                'table' => 'categories',
                'alias' => 'Category',
                'type' => 'inner',
                'conditions'=> array('Category.id = CategoriesPost.category_id', 'Category.slug' => $slug)
            )
        )
    )
);

$posts = $this->paginate();

could anyone gimme a solution since i'm a newbie? many thanks...

© Stack Overflow or respective owner

Related posts about cakephp

Related posts about habtm