Monthly Archives: 七月 2017

yii2 GridView 增加非本Model的属性并可搜索

backend/models/PublicStorageSumSearch.php
<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\PublicStorageSum;

/**
* PublicStorageSumSearch represents the model behind the search form about `backend\models\PublicStorageSum`.
*/
class PublicStorageSumSearch extends PublicStorageSum
{


/**
* @inheritdoc
*/
public function rules()
{
return [
[[‘id’, ‘warehouse_id’, ‘qty’, ‘allocated_qty’], ‘integer’],
[[‘sku’, ‘name’, ‘warehouse’, ‘remark’, ‘update_time’,product_name], ‘safe’],
];
}

public function attributes()
{
return array_merge(parent::attributes(), [
‘product_name’,
]);
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return array_merge(parent::attributeLabels(), [
‘product_name’ => ‘产品中文名称’,
]);
}

/**
* @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 = self::find()
->from([‘PublicStorageSum’ => self::tableName()])
->select([
‘PublicStorageSum.*’,
‘Materiel.namech’,
])
->leftJoin([‘Materiel’ => Materiel::tableName()], ‘Materiel.sku = PublicStorageSum.sku’
);

// add conditions that should always apply here

$dataProvider = new ActiveDataProvider([
‘query’ => $query,
]);

$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;
}

// grid filtering conditions
$query->andFilterWhere([
‘id’ => $this->id,
‘warehouse_id’ => $this->warehouse_id,
‘qty’ => $this->qty,
‘allocated_qty’ => $this->allocated_qty,
‘update_time’ => $this->update_time,


]);

$query->andFilterWhere([‘like’, ‘PublicStorageSum.sku’, $this->sku])
->andFilterWhere([‘like’, ‘name’, $this->name])
->andFilterWhere([‘like’, ‘warehouse’, $this->warehouse])
->andFilterWhere([‘like’, ‘remark’, $this->remark])
->andFilterWhere([‘like’, ‘namech’, $this->getAttribute(‘product_name’)])

->orderBy(‘sku’);

return $dataProvider;
}
}

statics/themes/admin/views/public-storage-sum/index.php
<?= GridView::widget([
‘dataProvider’ => $dataProvider,
‘filterModel’ => $searchModel,
‘columns’ => [
[‘class’ => ‘yii\grid\SerialColumn’],
‘id’,
‘sku’,

[
‘attribute’ => ‘product_name’,

‘value’ => function($data){return backend\models\Materiel::find()->where([‘sku’=>$data->sku])->one()->namech;},
],
// [
// ‘label’=>’产品名称’,
// ‘class’ => ‘yii\grid\DataColumn’, //由于是默认类型,可以省略
// ‘value’ => function ($data) {
//
// return backend\models\Materiel::find()->where([‘sku’=>$data->sku])->one()->namech;
//
//
// }
// ],
‘warehouse_id’,
[
‘label’=>‘仓库中文名’,
‘class’ => ‘yii\grid\DataColumn’, //由于是默认类型,可以省略
‘value’ => function ($data) {
return Warehouse::findOne($data)->namech;
}
],
‘qty’,
[
‘label’=>‘此库存可用库存’,
‘class’ => ‘yii\grid\DataColumn’, //由于是默认类型,可以省略
‘value’ => function ($data) {

return backend\models\PublicStorageSum::sumAllocatedQty($data->warehouse_id,$data->sku);


}
],
‘allocated_qty’,

‘update_time’,


],
]); ?>