如何将列添加到可用于排序的 Yii2 查询生成器结果?
How to add column to Yii2 query builder result which could be used to order by?
我有一个 Yii2 查询对象,其中包含执行全文搜索的方法
public function search($search)
{
return $this->andWhere('MATCH(title, description, tags) AGAINST(:search) AS value', ['search' => $search])
->orderBy('value');
}
整个代码看起来像
$dataProvider = new ActiveDataProvider([
'query' => Video::find()->published()->search($search)
]);
当然这不起作用,因为 andWhere() 不允许 AS 值语句。有人可以告诉我如何解决这个问题吗?非常感谢。
如果你想使用“值”,那么你必须把它写在select语句中。您可以使用“having”子句对结果应用过滤器。
以下两种方法均可使用
1.
$query = Product::find()
->andWhere('MATCH ('.Product::tableName().'.name) AGAINST(:q)'
->params(['q' => $this->q])
->orderBy('(MATCH ('.Product::tableName().'.name) AGAINST(:q)) DESC');
2
$query = Product::find();
->select(Product::tableName().'.*, MATCH ('.Product::tableName().'.name) AGAINST(:q) as VL')
->having('VL > 5')
->params(['q' => $this->q]);
我想您可以将搜索部分包含在 select 子句中,以便对结果进行排序
Video::find()
->published()
->search($search)
->select(['*','MATCH(title, description, tags) AGAINST(:search) AS value'])
->orderBy('value');
但问题是绑定 :search
占位符的值(对此不确定),为此我想你可以使用 yii\db\Expression
class which takes params as second argument and this expression can be used in select()
方法,所以现在查询构建器看起来像
Video::find()
->published()
->search($search)
->select(['*',
new \yii\db\Expression('MATCH(title, description, tags) AGAINST(:search) AS value',['search' => $search])
])
->orderBy('value');
Soution 是参数方法:
$this->select(['*','MATCH(title, description, tags) AGAINST(:search) AS score'])
->andWhere('MATCH(title, description, tags) AGAINST(:search)', ['search' => $search])
->params(['search' => $search])
->orderBy(['score' => SORT_DESC]);
我有一个 Yii2 查询对象,其中包含执行全文搜索的方法
public function search($search)
{
return $this->andWhere('MATCH(title, description, tags) AGAINST(:search) AS value', ['search' => $search])
->orderBy('value');
}
整个代码看起来像
$dataProvider = new ActiveDataProvider([
'query' => Video::find()->published()->search($search)
]);
当然这不起作用,因为 andWhere() 不允许 AS 值语句。有人可以告诉我如何解决这个问题吗?非常感谢。
如果你想使用“值”,那么你必须把它写在select语句中。您可以使用“having”子句对结果应用过滤器。
以下两种方法均可使用
1.
$query = Product::find()
->andWhere('MATCH ('.Product::tableName().'.name) AGAINST(:q)'
->params(['q' => $this->q])
->orderBy('(MATCH ('.Product::tableName().'.name) AGAINST(:q)) DESC');
2
$query = Product::find();
->select(Product::tableName().'.*, MATCH ('.Product::tableName().'.name) AGAINST(:q) as VL')
->having('VL > 5')
->params(['q' => $this->q]);
我想您可以将搜索部分包含在 select 子句中,以便对结果进行排序
Video::find()
->published()
->search($search)
->select(['*','MATCH(title, description, tags) AGAINST(:search) AS value'])
->orderBy('value');
但问题是绑定 :search
占位符的值(对此不确定),为此我想你可以使用 yii\db\Expression
class which takes params as second argument and this expression can be used in select()
方法,所以现在查询构建器看起来像
Video::find()
->published()
->search($search)
->select(['*',
new \yii\db\Expression('MATCH(title, description, tags) AGAINST(:search) AS value',['search' => $search])
])
->orderBy('value');
Soution 是参数方法:
$this->select(['*','MATCH(title, description, tags) AGAINST(:search) AS score'])
->andWhere('MATCH(title, description, tags) AGAINST(:search)', ['search' => $search])
->params(['search' => $search])
->orderBy(['score' => SORT_DESC]);