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:

  1. query companies based on name (name :name')
  2. priority 1 has valid , date (from_date , to_date)
  3. 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

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -