fitness-web/backend/models/WasteSearch.php
2016-05-08 08:56:42 +02:00

118 lines
3.1 KiB
PHP

<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Waste;
use yii\db\Query;
use common\components\Helper;
/**
* WasteSearch represents the model behind the search form about `common\models\Waste`.
*/
class WasteSearch extends Waste
{
public $date_start;
public $date_end;
public $timestampStart;
public $timestampEnd;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id_waste', 'count', 'id_product', 'stock_before', 'stock_after', 'id_user'], 'integer'],
[['comment', 'created_at', 'updated_at'], 'safe'],
[[ 'date_start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
[[ 'date_end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
// $query = Waste::find();
$query = new Query();
$query->select([
"p.name as product_name",
'w.count as waste_count',
'w.created_at as waste_created_at',
'w.comment as waste_comment',
'w.stock_before as waste_stock_before',
'w.stock_after as waste_stock_after',
'u.username as user_username',
]);
$query->from("waste as w");
$query->innerJoin("product as p","p.id_product = w.id_product");
$query->innerJoin("user as u","u.id = w.id_user");
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' =>[
'defaultOrder' => [
'waste_created_at' => SORT_DESC
],
'attributes' => Helper::mkYiiSortItems([
['product_name'],
['waste_count'],
['waste_created_at'],
['waste_comment'],
['waste_stock_before'],
['waste_stock_after'],
['user_username'],
])
]]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'w.id_user' => $this->id_user,
'w.id_product' => $this->id_product,
]);
if ( empty($this->date_start) ){
$this->timestampStart = '';
}
if ( empty($this->date_end) ){
$this->timestampEnd = '';
}
$query->andFilterWhere([ '>=', 'w.created_at', $this->timestampStart ] );
$query->andFilterWhere([ '<', 'w.created_at', $this->timestampEnd ] );
$query->andFilterWhere(['like', 'w.comment', $this->comment]);
return $dataProvider;
}
}