我有两张桌子:帖子和喜欢
我正在尝试列出当前登录用户喜欢的帖子。该方法如下所示:
$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());
所以基本上我要做的是首先向用户查询所有匹配行的以下(喜欢)表,然后在我的查询查询中使用这个post id数组来列出查询中的帖子。 / p>
Follower模型中的listFollowing方法如下所示:
public function listFollowing($user_id)
{
return $this->find('all', array(
'conditions' => array('Follower.user_id'=>$user_id)
));
}
我目前收到的错误如下:Undefined index: Follower [APP/Controller/PostsController.php, line 94]
因此我假设我在查找查询中尝试传递以下帖子ID列表的方式不正确。
有人可以帮忙吗?感谢
编辑:对$ following进行调试后给出:
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'
)
)
)
答案 0 :(得分:1)
几个问题:
在填充后是$?如果你调试它会发生什么?
如果您使用'all'进行检索,结果将如下所示:
阵列(
[0] => Array
(
[ModelName] => Array
(
[id] => 83
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 1
[field1] => value1
[field2] => value2
[field3] => value3
)
)
)
所以你永远无法在$'追随者'后面找到一些东西。检查Cake's documentation是否
根据您的评论,如果您需要一个id列表,请在您的方法中尝试:
public function listFollowing($user_id)
{
return $this->find('list', array(
'conditions' => array('Follower.user_id'=>$user_id)
'fields' => array('Follower.user_id'),
'recursive' => 0);
}
'list'检索模式就是这样。
答案 1 :(得分:0)
很难用这个有限的信息来猜测错误,但我猜这是与你的$following
数组相关的PHP错误。
您可以尝试在debug($following)
函数调用之后添加listFollowing()
吗?所以最后,它应该是这样的:
$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());
您将看到listFollowing()
返回的导致索引错误的内容。您可以使用该信息进一步调试它。由于没有数据库架构,完整代码,导致此问题的变量/情况等,我无法再提供帮助:)