yii增加检索条件字段
视图中增加检索框
$form->field($model, 'order_no_str')
增加公共属性
public $order_no_str = '';
获取参数并添加检索条件
在 $this->load($params);
$whereOrder = [];
if(!empty($this->order_no_str)){
$whereOrder = ['like','order.order_no',$this->order_no_str];
}
$query->andFilterWhere($whereOrder);
完整检索model
/**
* @Author: Wang chunsheng email:2192138785@qq.com
* @Date: 2021-01-02 14:23:57
* @Last Modified by: Wang chunsheng email:2192138785@qq.com
* @Last Modified time: 2021-01-02 18:37:15
*/
namespace addons\diandi_distribution\models\Searchs\account;
use yii\base\Model;
use common\components\DataProvider\ArrayDataProvider;
use addons\diandi_distribution\models\account\DistributionAccountOrder as DistributionAccountOrderModel;
use yii\data\Pagination;
/**
* DistributionAccountOrder represents the model behind the search form of `common\addons\diandi_distribution\models\account\DistributionAccountOrder`.
*/
class DistributionAccountOrder extends DistributionAccountOrderModel
{
public $order_no_str = '';
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'status', 'memberc_id', 'member_id', 'is_count', 'type', 'order_goods_id', 'order_type', 'goods_type', 'order_id', 'goods_id', 'update_time', 'create_time'], 'integer'],
[['performance', 'order_price', 'goods_price', 'money'], 'number'],
[['order_no_str'], 'safe'],
];
}
/**
* {@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)
{
global $_GPC;
$query = DistributionAccountOrderModel::find();
$query->joinWith('order as order');
$query->with([
'orderGoods',
'goodsSpec','goodsSpecRel','goodsShare',
'memberc','member',
'levelc','level'
]);
$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 false;
}
$whereOrder = [];
if(!empty($this->order_no_str)){
$whereOrder = ['like','order.order_no',$this->order_no_str];
}
$query->andFilterWhere($whereOrder);
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'status' => $this->status,
'memberc_id' => $this->memberc_id,
'member_id' => $this->member_id,
'is_count' => $this->is_count,
'type' => $this->type,
'performance' => $this->performance,
'order_goods_id' => $this->order_goods_id,
'order_type' => $this->order_type,
'goods_type' => $this->goods_type,
'order_id' => $this->order_id,
'order_price' => $this->order_price,
'goods_id' => $this->goods_id,
'goods_price' => $this->goods_price,
'money' => $this->money,
'update_time' => $this->update_time,
'create_time' => $this->create_time,
]);
$count = $query->count();
$pageSize = $_GPC['pageSize'];
$page = $_GPC['page'];
// 使用总数来创建一个分页对象
$pagination = new Pagination([
'totalCount' => $count,
'pageSize' => $pageSize,
'page' => $page - 1,
// 'pageParam'=>'page'
]);
$list = $query->offset($pagination->offset)
->limit($pagination->limit)
->all();
//foreach ($list as $key => &$value) {
// $value['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
// $value['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
//}
$provider = new ArrayDataProvider([
'key'=>'id',
'allModels' => $list,
'totalCount' => isset($count) ? $count : 0,
'total'=> isset($count) ? $count : 0,
'sort' => [
'attributes' => [
//'member_id',
],
'defaultOrder' => [
//'member_id' => SORT_DESC,
],
],
'pagination' => [
'pageSize' => $pageSize,
]
]);
return $provider;
}
}