Reputation: 5962
So, I'ld like to paginate one of my request. The "normal way" didn't seems to work very well, so, I made a little research and all the answers ware telling to do something like this :
$this->paginate("Like",
array("Like.user_id" => $id),
array(
"order" => "Like.created desc",
"contain" => array(
"User",
"Post",
"Post.User",
"Post.Like"
)
)
);
The problem is that ... it doesn't work. A debug
gives me something like that :
array(
(int) 0 => array(
'Like' => array(
'id' => '38',
'created' => '2012-10-03 21:21:27',
'post_id' => '29',
'user_id' => '19'
),
'Post' => array(
'id' => '29',
'title' => 'Don't Panic',
'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam elementum sem ac sem imperdiet cursus. Quisque venenatis pulvinar ornare. Donec rutrum, lacus vel imperdiet sagittis, metus risus interdum ante, iaculis venenatis arcu nibh et odio.',
'image' => '29-c49cb96c.jpg',
'model' => '',
'created' => '2012-09-07 01:46:49',
'user_id' => '19',
'project_id' => '1',
'like_count' => '1'
),
'User' => array(
'password' => '*****',
'id' => '19',
'username' => 'Axiol'
)
)
)
No traces of Post.User
and the Post.Like
...
Any idea ?
EDIT:
Here are the models associations :
Post
public $belongsTo = array("User", "Project");
public $hasMany = array("Comment", "Like");
Like
public $belongsTo = array("User", "Post" => array("counterCache" => true));
User
public $hasMany = array("Post", "Comment");
Upvotes: 1
Views: 7050
Reputation: 368
First off, you should indeed correct your contain
array as Oldskool answered.
This should have been very obvious because cake throughs something like this:
Warning (512): Model "Post.Like" is not associated with model "Like"
[CORE\Cake\Model\Behavior\ContainableBehavior.php, line 342]
You probably didn't see that because of your actual problem.
You fail to mention whether you are in any way loading the Containable
behavior.
I'm assuming you aren't, and there you have it. The contain
array is being ignored altogether.
Here is the documentation for the Containable
behavior.
My recommendation is that you add it to your AppModel::actsAs
property like so:
class AppModel extends Model {
public $actsAs = array('Containable');
}
This way, it is automatically available to all your models and you should stop using recursive
madness too.
Hope this helps.
Upvotes: 2
Reputation: 34837
As Dave already mentioned, your model names are off. You shouldn't use that notation, but rather include them as "nested" model underneath the one you're loading. Like this:
"contain" => array(
"User",
"Post" => array(
"User", // Get post related user data
"Like" // Get post related like data
)
)
Upvotes: 1
Reputation: 29121
Remove the Post.User
and Post.Like
- those aren't models and shouldn't be listed in the contain
array.
If they ARE model names - ie, you've specified the $name
as such in your models (even though it breaks naming convention), then please post your models and their associations and we can look further.
Upvotes: 1