php - How to use order by field in yii framework
黄舟
黄舟 2017-06-20 10:07:49
0
1
1183

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###
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
三叔

->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')")])
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!