Complicated sorting based on date Yii2 -
i creating search in yii2.
$q = $_get['q']; $dataprovider = new activedataprovider([ 'query' => company::find() ->where('name :name', [':name' => "$q%"]) ]); return $this->render('index', ['dataprovider' => $dataprovider,]);
i have 2 date properties (valid payment , to):
from_date , to_date
i need display result starting 1 has valid payment first.
how should qyery if want display companies valid payment first? should sort on name asc..
i have function value in model, not query:
public function getvalidpayment() { $today = date("y-m-d"); return (($this->from_date <= $today) && ($today <= $this->to_date)); }
this yii2
thanks!
edit: clarify need:
- query companies based on name (name :name')
- priority 1 has valid , date (from_date , to_date)
- display rest not have valid , date (both groups in query should sort on name asc).
maybe should in 2 queries, , somehow combine them?
'query' => company::find() ->select([ 'valid_payment'=>"(('".$today."'<=to_date) , (from_date<='".$today."'))", name, //...some fields required in grid ]) ->where('name :name', [':name' => "$q%"]) ->orderby('valid_payment desc, name asc')
and don't forget initialize $today
Comments
Post a Comment