如何通过另一个相关表(一对一关系)从相关表中获取和搜索属性



我有三个表如下:

策略:(id, r_available_taemin[fk]( --

available_taemin:(id, , name, r_company[fk]( --

公司:(ID,名称(

在策略(索引(的网格视图中,我需要显示公司名称,我已经这样做了。 但是现在我需要为公司名称添加过滤,在这里我遇到了问题。

我的搜索模型是广告如下:

<?php
namespace appmodels;
use Yii;
use yiibaseModel;
use yiidataActiveDataProvider;
use appmodelsappmodelsAppPoliciesGeneral;
/**
* PoliciesGeneralSearch represents the model behind the search form of `appmodelsappmodelsAppPoliciesGeneral`.
*/
class PoliciesGeneralSearch extends AppPoliciesGeneral {
public $customerName;
public $companyName;
/**
* @inheritdoc
*/
public function rules() {
return [
[['policy_id', 'r_deal_type', 'r_customer_id', 'r_available_taemin', 'is_active', 'r_invoice', 'isRenewed'], 'integer'],
[['policy_type', 'policy_code', 'madmoun_name', 'payment_option', 'contract_date', 'start_date', 'end_date', 'attachment', 'sale_letters', 'paid_customer_letters', 'paid_company_letters', 'table_name', 'deactivated_at', 'created_at', 'updated_at'], 'safe'],
[['cost', 'price', 'sale', 'tax', 'paid_customer', 'remaining_customer', 'paid_company', 'remaining_company'], 'number'],
[['customerName', 'companyName'], '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 = AppPoliciesGeneral::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
$query->joinWith(['rCustomer']);
$query->joinWith(['rAvailableTaemin']);
//        $query->joinWith(['rCompany']);
$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([
'policy_id' => $this->policy_id,
'r_deal_type' => $this->r_deal_type,
'r_customer_id' => $this->r_customer_id,
'r_available_taemin' => $this->r_available_taemin,
'contract_date' => $this->contract_date,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'is_active' => $this->is_active,
'cost' => $this->cost,
'price' => $this->price,
'sale' => $this->sale,
'tax' => $this->tax,
'paid_customer' => $this->paid_customer,
'remaining_customer' => $this->remaining_customer,
'paid_company' => $this->paid_company,
'remaining_company' => $this->remaining_company,
'r_invoice' => $this->r_invoice,
'deactivated_at' => $this->deactivated_at,
'isRenewed' => $this->isRenewed,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere([ 'like', 'policy_type', $this->policy_type])
->andFilterWhere([ 'like', 'policy_code', $this->policy_code])->andFilterWhere(['like', 'madmoun_name', $this->madmoun_name])->andFilterWhere(['like', 'payment_option', $this->payment_option])->andFilterWhere(['like', 'attachment', $this->attachment])->andFilterWhere(['like', 'sale_letters', $this->sale_letters])->andFilterWhere(['like', 'paid_customer_letters', $this->paid_customer_letters])->andFilterWhere(['like', 'paid_company_letters', $this->paid_company_letters])->andFilterWhere(['like', 'table_name', $this->table_name]);
$query->joinWith(['rCustomer' => function ($q) {
$q->where('customers.first_name LIKE "%' . $this->customerName . '%"' .
'OR customers.fathers_name LIKE "%' . $this->customerName . '%"' .
'OR customers.last_name LIKE "%' . $this->customerName .  '%"'
);
}]);
//        $query->joinWith(['rAvailableTaemin' => function ($q) {
//                $q->where('companies.reprisentative_name LIKE "%' . $this->companyName . '%"');
//            }]);
//        $query->joinWith(['rCompany' => function ($q) {
//                $q->where('companies.name LIKE "%' . $this->companyName . '%"');
//            }]);
//        $query->joinWith(['companies' => function ($q) {
//                $q->where('companies.name LIKE "%' . $this->companyName . '%"');
//            }]);
return $dataProvider;
}
}

到目前为止,我的模型如下:

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace appmodelsappmodels;
use appmodelsPoliciesGeneral;
use Yii;
/**
* Description of AppPoliciesGeneral
*
* @author BigUser
*/
class AppPoliciesGeneral extends PoliciesGeneral {
public function getRCustomer() {
return $this->hasOne(AppCustomers::className(), ['id' => 'r_customer_id']);
}
public function getCustomerName() {
//        return $this->rCustomer->first_name . " " . $this->rCustomer->fathers_name . " " . $this->rCustomer->last_name . "";
return $this->rCustomer->fullName;
}
public function getRAvailableTaemin() {
return $this->hasOne(AppAvailableTaemin::className(), ['id' => 'r_available_taemin']);
}
//public function getRCompany() {
//        return $this->hasMany(AppCompanies::className(), ['r_company' => 'id'])
//                        ->viaTable('available_taemin', ['r_available_taemin' => 'id']);
//    }
public function getCompanyid() {
return $this->rAvailableTaemin->r_company;
}
public function getRCompany() {
return $this->hasOne(AppCompanies::className(), ['id' => 'companyid']);
}
public function getCompanyName() {
return $this->rCompany->name;
}
//    public function getCompanyName() {
//        return $this->rAvailableTaemin->companyName;
//    }

}

提前谢谢。

我不知道这里有什么问题。查看您的代码,我可以看到过滤公司名称,但它被注释掉了,为什么?

无论如何,一些提示:

您不必设置所有属性进行排序,它是自动完成的,只是不要像这里那样覆盖它。由于您有两个虚拟属性,因此请执行以下操作:

$dataProvider->sort->attributes['companyName'] = [
'asc' => ['companies.name' => SORT_ASC],
'desc' => ['companies.name' => SORT_DESC],
'label' => 'Company Name',
'default' => SORT_ASC
];
$dataProvider->sort->attributes['customerName' = [
'asc' => ['customers.first_name' => SORT_ASC, 'customers.fathers_name' => SORT_ASC, 'customers.last_name' => SORT_ASC],
'desc' => ['customers.first_name' => SORT_DESC, 'customers.fathers_name' => SORT_DESC, 'customers.last_name' => SORT_DESC],
'label' => Yii::t('app', 'Customer Name'),
];

仅此而已。其余属性将添加到自动排序中。

客户过滤 - 已经添加了关系,您不必再次这样做。

if (!empty($this->customerName)) {
$query->andWhere([
'or',
['like', 'customers.first_name', $this->customerName],
['like', 'customers.fathers_name', $this->customerName],
['like', 'customers.last_name', $this->customerName],
]);
}

现在公司过滤,这更容易,因为您只检查一列:

$query->andFilterWhere(['like', 'companies.name', $this->companyName]);

最新更新