search

Home  >  Q&A  >  body text

php - How to use order by field in yii framework

How to splice order by field in orderBy in yii,

, now the printed sql, and the sql I need is like this

, please tell me how to solve this problem, this is the reference in mysql Order by field (id, 5, 3, 8) Another point, in the Yii framework, I want to implement a special sorting of a certain field. Is there any other method besides order by field? I checked several methods and it is all due to "` "Error caused by this symbol###
黄舟黄舟2712 days ago1196

reply all(1)I'll reply

  • 三叔

    三叔2017-06-20 10:09:49

    ->orderBy(["FIELD(step, 'star', 'person', 'team')" => true]) is OK, but I didn't look at the underlying code carefully. . . But it should solve the problem

    /**
         * @param array $columns
         * @return string the ORDER BY clause built from [[Query::$orderBy]].
         */
        public function buildOrderBy($columns)
        {
            if (empty($columns)) {
                return '';
            }
            $orders = [];
            foreach ($columns as $name => $direction) {
                if ($direction instanceof Expression) {
                    $orders[] = $direction->expression;
                } else {
                    $orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : '');
                }
            }
    
            return 'ORDER BY ' . implode(', ', $orders);
        }
    

    This is a method to generate an orderby statement, so true has no real effect, but it is not equal to SORT_DESC and then becomes empty,

    The more official way of writing it is like this:

    orderBy([new Expression("FIELD(step, 'star', 'person', 'team')")])

    reply
    0
  • Cancelreply