|
|
<?php
namespace common\models\searchs;
use yii\base\Model; use yii\data\ActiveDataProvider; use yii\helpers\ArrayHelper; use common\models\ars\Goods;
/** * GoodsSearch represents the model behind the search form of `common\models\ars\Goods`. */ class GoodsSearch extends Goods { /** * @return array * 增加创建时间查询字段 */ public function attributes() { return ArrayHelper::merge(['created_at_range'], parent::attributes()); } /** * {@inheritdoc} */ public function rules() { return [ [['id', 'pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'market_price', 'price', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template', 'created_at', 'updated_at'], 'integer'], [['name', 'sn', 'code', 'unit', 'brief', 'description'], 'safe'], ['created_at_range','safe'], ]; }
/** * {@inheritdoc} */ public function scenarios() { // bypass scenarios() implementation in the parent class
return Model::scenarios(); } /** * @return array * 列格式 */ public function columns() { return [ [ 'class' => 'blobt\grid\CheckboxColumn', 'width' => '2%', 'align' => 'center' ], 'id', 'pid', 'cat_id', 'brand_id', 'shop_cat_id', //'name',
//'sn',
//'code',
//'supplier_id',
//'weight',
//'length',
//'width',
//'height',
//'diameter',
//'unit',
//'sold_count',
//'limit_count',
//'stock',
//'stock_warn',
//'market_price',
//'price',
//'brief',
//'description',
//'image',
//'model_id',
//'is_sale',
//'sort_order',
//'bouns_points',
//'experience_points',
//'is_delete',
//'express_template',
//'created_at',
//'updated_at',
[ 'class' => 'iron\grid\ActionColumn', 'align' => 'center', ], ]; } /** * @param $params * @return ActiveDataProvider * 不分页的所有数据 */ public function allData($params) { $query = Goods::find(); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => false, 'sort' => false ]); $this->load($params); return $this->filter($query, $dataProvider); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Goods::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSizeLimit' => [1, 200] ], 'sort' => [ 'defaultOrder' => [ 'id' => SORT_DESC, ] ], ]);
$this->load($params); return $this->filter($query, $dataProvider); } /** * @param $query * @param $dataProvider * @return ActiveDataProvider * 条件筛选 */ private function filter($query, $dataProvider){ 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; }
// grid filtering conditions
$query->andFilterWhere([ 'id' => $this->id, 'pid' => $this->pid, 'cat_id' => $this->cat_id, 'brand_id' => $this->brand_id, 'shop_cat_id' => $this->shop_cat_id, 'supplier_id' => $this->supplier_id, 'weight' => $this->weight, 'length' => $this->length, 'width' => $this->width, 'height' => $this->height, 'diameter' => $this->diameter, 'sold_count' => $this->sold_count, 'limit_count' => $this->limit_count, 'stock' => $this->stock, 'stock_warn' => $this->stock_warn, 'market_price' => $this->market_price, 'price' => $this->price, 'image' => $this->image, 'model_id' => $this->model_id, 'is_sale' => $this->is_sale, 'sort_order' => $this->sort_order, 'bouns_points' => $this->bouns_points, 'experience_points' => $this->experience_points, 'is_delete' => $this->is_delete, 'express_template' => $this->express_template, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]);
$query->andFilterWhere(['like', 'name', $this->name]) ->andFilterWhere(['like', 'sn', $this->sn]) ->andFilterWhere(['like', 'code', $this->code]) ->andFilterWhere(['like', 'unit', $this->unit]) ->andFilterWhere(['like', 'brief', $this->brief]) ->andFilterWhere(['like', 'description', $this->description]); if ($this->created_at_range) { $arr = explode(' ~ ', $this->created_at_range); $start = strtotime($arr[0]); $end = strtotime($arr[1]) + 3600 * 24; $query->andFilterWhere(['between', 'created_at', $start, $end]); } return $dataProvider; } }
|