我正在yii2中创建搜索。
$q = $_GET['q'];
$dataProvider = new ActiveDataProvider([
'query' => company::find()
->where('name like :name', [':name' => "$q%"])
]);
return $this->render('index', ['dataProvider' => $dataProvider,]);
我还有两个日期属性(来自和来的有效付款):
from_date and to_date
我需要首先显示有效付款的结果。
如果我想首先显示有效付款的公司,那么qyery应该怎么看?它们应该都取名为ASC ..
我有一个函数可以在模型中获取此值,但不是查询:
public function getValidPayment()
{
$today = date("Y-m-d");
return (($this->from_date <= $today) && ($today <= $this->to_date));
}
这是yii2
谢谢!
编辑:只是为了澄清我需要的东西:
也许我应该在2个查询中执行此操作,并以某种方式将它们组合在一起?
答案 0 :(得分:1)
'query' => company::find()
->select([
'valid_payment'=>"(('".$today."'<=to_date) AND (from_date<='".$today."'))",
name,
//...some fields you required in your grid
])
->where('name like :name', [':name' => "$q%"])
->orderBy('valid_payment DESC, name ASC')
并且不要忘记今天初始化$