Cakephp paginate multiple habtm?

I've run into a similar problem, when trying to paginate a resultset using an associated model. I ended up having to manually bind my models together, run the query, and then unbind them in order to get Cake to contain the right data together. ( book.cakephp.org/view/86/Creating-and-De... ).

Up vote 0 down vote favorite share g+ share fb share tw.

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... cakephp habtm link|improve this question asked Mar 14 '10 at 23:10izmanromli12.

– sibidiba Mar 15 '10 at 2:53 Well if I fetch Tag, then the result is a loop of the first Post. But if no Tag were fetched, then it returns all posts in a correct result. – izmanromli Mar 16 '10 at 2:13.

I've run into a similar problem, when trying to paginate a resultset using an associated model. I ended up having to manually bind my models together, run the query, and then unbind them in order to get Cake to contain the right data together. ( book.cakephp.org/view/86/Creating-and-De... ) You could also try the containable behaviour which will allow you to specify which models you want to include in your result set.

Containable is core in 1.2+ ( book.cakephp.org/view/474/Containable ), otherwise you'll need to grab it yourself. I'm not too sure on why you have such a gargantuan query there though. I would be more inclined to do something similar to the following.

$this->Model->recursive = 2; $this->Model->paginate(); And let Cake get all the related data for me through my associations. Then I would adjust the return using a conditions array ( api.cakephp.org/class/controller#method-... ) to specify the category. Sorry it's not a defacto solution, but I'm a CakePHP amateur myself!

You might find it easier to view the queries, results etc, using DebugKit, http://github.com/cakephp/debug_kit.

I use Containable to fetch Tag and joins table for Category. Not the best solution for me but at least I get what I want – izmanromli Mar 17 '10 at 4:56.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions