Paw*_*wan 3 php gridview yii yii2
我试图在Yii2的GridView小部件中为相关模型设置过滤器,但我不断得到错误,因为过滤器值必须是整数.
我跟着这个问题.现在,我有两个型号Services.php和ServiceCharge.php.
在ServiceCharge.php关系设置如下:
public function getServiceName()
{
return $this->hasOne(Services::className(),['id'=>'service_name']);
}
Run Code Online (Sandbox Code Playgroud)
在ServiceChargeSearch.php代码中是这样的:
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\ServiceCharges;
/**
* ServiceChargesSearch represents the model behind the search form about `app\models\ServiceCharges`.
*/
class ServiceChargesSearch extends ServiceCharges
{
/**
* @inheritdoc
*/
public function attributes()
{
// add related fields to searchable attributes
return array_merge(parent::attributes(), ['serviceName.services']);
}
public function rules()
{
return [
[['id'], 'integer'],
[['charges_cash', 'charges_cashless'], 'number'],
[['id', 'serviceName.services', 'room_category'], '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)
{
$query = ServiceCharges::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['serviceName.services'] = [
'asc' => ['serviceName.services' => SORT_ASC],
'desc' => ['serviceName.services' => SORT_DESC],
];
$query->joinWith(['serviceName']);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
// 'service_name' => $this->service_name,
'room_category' => $this->room_category,
'charges_cash' => $this->charges_cash,
'charges_cashless' => $this->charges_cashless,
])
->andFilterWhere(['LIKE', 'serviceName.services', $this->getAttribute('serviceName.services')]);
return $dataProvider;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的Gridview中,它设置如下:
[
'attribute'=>'service_name',
'value'=>'serviceName.services',
],
Run Code Online (Sandbox Code Playgroud)
这是正确显示相关模型的服务名称.
我无法看到我做错了什么,但服务属性的过滤器字段根本没有显示.
Paw*_*wan 11
实际上它比看起来简单得多.
添加column_name到安全属性.注意:这应该是名称关系
添加查询连接 - 如 - $query->joinWith(['serviceName','roomCategory']);
添加过滤条件如:
->andFilterWhere(['like', 'services.services', $this->service_name])
->andFilterWhere(['like', 'room_category.room_category', $this->room_category]);
Run Code Online (Sandbox Code Playgroud)如果要添加排序添加代码如:
$dataProvider->sort->attributes['service_name'] = [
'asc' => ['services.services' => SORT_ASC],
'desc' => ['services.services' => SORT_DESC],
];
$dataProvider->sort->attributes['room_category'] = [
'asc' => ['room_category.room_category' => SORT_ASC],
'desc' => ['room_category.room_category' => SORT_DESC],
];
Run Code Online (Sandbox Code Playgroud)5你还应该设置关系名称 public $roomCategory
而已.相关表的排序和过滤都很有效.
注意:删除默认验证,如相关列的整数和默认过滤,gii否则会产生错误.
最新版本更新:
就是这个例子
[
'attribute=>'attribute_name',
'value=function($data){
return $data->relationname->related_table_attribute_name
}
],
Run Code Online (Sandbox Code Playgroud)
记住你使用relation_name.related_table_attribute_name过滤器不知何故对我不起作用.
| 归档时间: |
|
| 查看次数: |
8840 次 |
| 最近记录: |