【问题标题】:Sort forum topics by messages date relationship in cakephp在cakephp中按消息日期关系对论坛主题进行排序
【发布时间】:2016-02-24 17:33:09
【问题描述】:

我正在使用 cakephp 2 框架。

我有一个具有“hasmany”关系的模型“主题”:

public $hasMany=array(
                    'Messages' => array(
                                        'className' => 'Messages',
                                        'foreignKey'=>'id_topic',
                                        'dependent'=>true,
                                    ),

                    );

在我的控制器中我这样做:

$this->paginate = array(
                    'Topic'    => array(
                            'limit'    => 10,
                            'recursive' => 2,

                        )
                );

$entities = $this->paginate('Topic');

它工作得很好,但现在我想按最后一条消息的日期对我的主题进行排序,但我不知道如何。

类似:

$this->paginate = array(
                    'Topic'    => array(
                            'limit'    => 10,
                            'recursive' => 2,
                            'order'=>'Messages.created desc'

                        )
                );

但我得到Column not found: 1054 Unknown column 'Messages.created' in 'order clause'

有什么想法吗?谢谢

【问题讨论】:

  • 你试过这个'order' => array('Messages.created' => 'desc')

标签: php cakephp cakephp-2.0 relationship


【解决方案1】:

好的,现在我正在为我展示工作代码,也许它可以帮助你

$this->Topic->find('all');
        $this->paginate = array(
                            //'conditions' => array('id !=' => '6'),
                            'limit' => 6,
                            'order' => array('Topic.modified' => 'desc')
                            );
    $data = $this->paginate('Topic');

在我的情况下,我的topics 表中有模型Topic,字段modified

【讨论】:

  • 我想按主题上最后一条消息的日期而不是主题的日期排序;)但感谢帮助我
【解决方案2】:

最后,我在主题表“last_message_date”中添加一列

$this->paginate = array(
                    'Topic'    => array(
                            'limit'    => 10,
                            'recursive' => 2,  
                           'order'=>array('last_message_date'=>'desc')
                        )
                );

不是一个真正合适的解决方案,但我不知道

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2015-02-27
    • 2020-10-30
    相关资源
    最近更新 更多