2

我有两个表:帖子和喜欢

我正在尝试列出当前登录用户喜欢的帖子。该方法如下所示:

$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());

所以基本上我要做的是首先查询以下(喜欢)表以获取与用户匹配的所有行,然后在我的查找查询中使用这个帖子 ID 数组来列出查询中的帖子。

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'
        )
    )
)
4

2 回答 2

1

几个问题:

  • 您是否检查了正在执行的查询?如果它被执行,那么该方法可能是正确的。
  • $following 是否被填充?如果你调试它会发生什么?

  • 如果您使用 'all' 进行检索,则结果将如下所示:

大批(

    [0] => Array
        (
            [ModelName] => Array
                (
                    [id] => 83
                    [field1] => value1
                    [field2] => value2
                    [field3] => value3
                )

            [AssociatedModelName] => Array
                (
                    [id] => 1
                    [field1] => value1
                    [field2] => value2
                    [field3] => value3
                )

        )
)

因此,您将永远无法在 $following['Follower'] 中找到任何东西。检查Cake 的文档

根据您的评论,如果您需要一个 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);
}

“列表”检索模式正是这样做的。

于 2012-12-15T23:22:13.280 回答
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()内容导致了索引错误。您可以使用该信息进一步调试它。我无法再提供帮助,因为没有数据库架构、完整代码、导致此问题的变量/情况等 :)

于 2012-12-15T23:21:13.433 回答