Reputation: 28803
I have two tables: Posts and Likes
And I'm trying to list the posts that the current logged in user likes. The method looks like the following:
$following = $this->Follower->listFollowing($this->Auth->user('id'));
$this->paginate = array('limit'=>20,'conditions'=>array('Post.id'=>array($following['Follower']['post_id']),'Post.status'=>array(1,2)),'order'=>array('Post.datetime'=>'desc'),
'contain'=>array('User','Answer'=>array('User'),'Tag'));
$this->set('posts',$this->paginate());
So basically what I'm trying to do is first query the following (likes) table for all matching rows to the user and then use this array of post ids in my find query to list posts that were in the query.
The listFollowing method in the Follower model looks like:
public function listFollowing($user_id)
{
return $this->find('all', array(
'conditions' => array('Follower.user_id'=>$user_id)
));
}
I'm currently getting an error like: Undefined index: Follower [APP/Controller/PostsController.php, line 94]
So gonna presume that the way I'm trying to pass the list of post ids from the following in the find query is incorrect.
Can anyone help? Thanks
Edit: Doing a debug on $following gives:
array(
(int) 0 => array(
'Follower' => array(
'id' => '4',
'user_id' => '6',
'post_id' => '136'
),
'User' => array(
'password' => '*****',
'id' => '6',
'username' => 'driz',
'email' => '######'
),
'Post' => array(
'id' => '136',
'user_id' => '8',
'datetime' => '2012-09-11 15:49:52',
'modified' => '2012-09-16 15:31:38',
'title' => 'Test Content',
'slug' => 'Test_content',
'content' => 'Test Content',
'status' => '1'
)
),
(int) 1 => array(
'Follower' => array(
'id' => '5',
'user_id' => '6',
'post_id' => '133'
),
'User' => array(
'password' => '*****',
'id' => '6',
'username' => 'driz',
'email' => '######'
),
'Post' => array(
'id' => '134',
'user_id' => '8',
'datetime' => '2012-09-11 15:49:52',
'modified' => '2012-09-16 15:31:38',
'title' => 'Test Content 2',
'slug' => 'Test_content_2',
'content' => 'Test Content 2',
'status' => '1'
)
)
)
Upvotes: 2
Views: 141
Reputation: 385
Couple of questions:
is $following being populated? what happens if you debug it?
if you are retreiving with 'all' then the result will look like this:
Array(
[0] => Array
(
[ModelName] => Array
(
[id] => 83
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 1
[field1] => value1
[field2] => value2
[field3] => value3
)
)
)
So you'll never be able to find something at $following['Follower']. Check Cake's documentation for this
Based on you comments, if you need a list of id's try this inside your method:
public function listFollowing($user_id)
{
return $this->find('list', array(
'conditions' => array('Follower.user_id'=>$user_id)
'fields' => array('Follower.user_id'),
'recursive' => 0);
}
The 'list' retrieve mode does exactlye that.
Upvotes: 1
Reputation: 320
It is hard to guess the error with this limited information but I'm guessing that it's a PHP error related with your $following
array.
Can you try putting debug($following)
after your listFollowing()
function call? So in the end, it should look like this:
$following = $this->Follower->listFollowing($this->Auth->user('id'));
debug($following);
$this->paginate = array('limit'=>20,'conditions'=>array('Post.id'=>array($following['Follower']['post_id']),'Post.status'=>array(1,2)),'order'=>array('Post.datetime'=>'desc'),
'contain'=>array('User','Answer'=>array('User'),'Tag'));
$this->set('posts',$this->paginate());
You will see what is returned from listFollowing()
that is causing an index error. You can further debug it with that information. I can no longer help as there is no database schema, full code, the variables/situation that causes this problem, etc :)
Upvotes: 0