gridview in php,php – 在GridView中过滤相关模型的设置

我试图在Yii2的

GridView小部件中为相关模型设置过滤器,但我不断得到错误,因为过滤器值必须是整数.

我已经关注了this question.现在,我有两个型号Services.php和ServiceCharge.php.

在ServiceCharge.php中,关系设置如下:

public function getServiceName()

{

return $this->hasOne(Services::className(),['id'=>'service_name']);

}

在ServiceChargeSearch.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;

}

}

在我的Gridview中它的设置如下:

[

'attribute'=>'service_name',

'value'=>'serviceName.services',

],

这是正确显示相关模型的服务名称.

我无法看到我做错了什么,但服务属性的过滤字段根本没有显示.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值