【问题标题】:how to use Orderby in cakephp如何在 cakephp 中使用 Orderby
【发布时间】:2018-04-20 05:11:22
【问题描述】:

我正在尝试按照优先级排序数据... 但是这个查询对我不起作用...... 我不知道哪里出错了..

请帮我解决这个问题..

$admins = $this->xxx->find()
    ->select($fields)
    ->where($conditions)
    ->contain([
        'yyy' => function ($q) {   
            return $q->autoFields(false)
                ->select(['id','name','login_url','priority'])
                ->order(['priority' => 'ASC']);
            }
    ])
    ->all();

【问题讨论】:

  • "不起作用" 不是正确的问题描述!请更具体一些,并解释究竟会发生什么(生成的 SQL 是什么样的,您收到什么数据等...),以及您期望发生的具体情况。

标签: cakephp cakephp-3.0 query-builder


【解决方案1】:

如果 xxx 和 yyy 之间的关系是 belongsTo 那么你必须将 order() 方法移到包含之外

$admins = $this->xxx->find()
    ->select($fields)
    ->where($conditions)
    ->contain([
        'yyy' => function ($q) {   
            return $q->autoFields(false)
                ->select(['id','name','login_url','priority']);
            }
    ])
    ->order(['yyy.priority' => 'ASC'])
    ->all();

【讨论】:

    【解决方案2】:
    array(
        'conditions' => array('Model.field' => $thisValue), //array of conditions
        'recursive' => 1, //int
        //array of field names
        'fields' => array('Model.field1', 'DISTINCT Model.field2'),
        //string or array defining order
        'order' => array('Model.created', 'Model.field3 DESC'),
        'group' => array('Model.field'), // fields to GROUP BY
        'limit' => n, //int
        'page' => n, //int
        'offset' => n, //int
        'callbacks' => true //other possible values are false, 'before', 'after'
        'having' => array('COUNT(Model.field) >' => 1), // fields to HAVING by
        'lock' => true // Enable FORM UPDATE locking
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 2014-10-18
      相关资源
      最近更新 更多