【问题标题】:List posts that a user likes in CakePHP app在 CakePHP 应用程序中列出用户喜欢的帖子
【发布时间】:2012-12-15 23:13:00
【问题描述】:

我有两个表:Posts 和 Likes

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

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

【问题讨论】:

    标签: php cakephp


    【解决方案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's documentation这个

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

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

    【讨论】:

    • @Cameron 那么我的猜测是正确的,没有 $following['Follower'] 它应该是 $followging[0]['Follower']
    • 如果我有很多赞怎么办? 0 仍然正确吗?因为我只得到第一个赞,而不是预期的完整列表。如果我用 1 替换 0 那么它会抓住下一个等等。我如何将它们全部拉下来?
    • 嘿,谢谢。不过还是一头雾水。该查找列表在哪里?它会在模型​​中替换 listFollowing 吗?然后我将如何在控制器中的查找查询中使用这些结果。如果您可以详细说明您的代码示例,那就太棒了。谢谢。
    • @Cameron 是的,检查我的答案我用完整的方法更新了它
    【解决方案2】:

    通过这些有限的信息很难猜出错误,但我猜这是与您的 $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() 返回的导致索引错误的内容。您可以使用该信息进一步调试它。我无法再提供帮助,因为没有数据库架构、完整代码、导致此问题的变量/情况等 :)

    【讨论】:

    • 你会得到一个追随者数组。因此,如果您想获取帖子列表,则需要遍历 $following 列表,获取所有 id 并查询帖子。您可能想使用foreach
    • @ErenT。你不觉得改变检索模式更好吗? “列表”模式就是这样做的,因此您不必遍历结果
    • @LuisClemente 对,但它不是仍然需要遍历才能获得完整帖子的数组吗?列表模式下,数组的结构变了,还是需要遍历的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2018-07-27
    • 2019-06-02
    • 2020-12-12
    • 2020-09-03
    • 2019-08-13
    • 2020-11-04
    相关资源
    最近更新 更多