Yii2 如何通过另一个相关表(一对一关系)从相关表中获取和搜索属性
我有以下三个表格: 策略:(id,r\u可用\u taemin[fk])-- 可用_taemin:(id、名称、r_公司[fk])-- 公司:(身份证,姓名) 在策略(索引)的网格视图中,我需要显示公司名称,我已经这样做了。 但是现在我需要为公司名称添加过滤,而这里我面临着一些问题 我的搜索模式如下:Yii2 如何通过另一个相关表(一对一关系)从相关表中获取和搜索属性,yii2,Yii2,我有以下三个表格: 策略:(id,r\u可用\u taemin[fk])-- 可用_taemin:(id、名称、r_公司[fk])-- 公司:(身份证,姓名) 在策略(索引)的网格视图中,我需要显示公司名称,我已经这样做了。 但是现在我需要为公司名称添加过滤,而这里我面临着一些问题 我的搜索模式如下: <?php namespace app\models; use Yii; use yii\base\Model; use yii\data\ActiveDataProvider; use
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\appmodels\AppPoliciesGeneral;
/**
* PoliciesGeneralSearch represents the model behind the search form of `app\models\appmodels\AppPoliciesGeneral`.
*/
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;
}
}
我不知道这里有什么问题。查看您的代码,我可以看到筛选公司名称,但它被注释掉了,为什么
总之,有几点建议:
您不必为排序设置所有属性,它是自动完成的,只是不要像这里那样覆盖它。由于您有两个虚拟属性,请执行以下操作:
$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]);
显示您的搜索模型。我面临错误:SQLSTATE[42S22]:未找到列:1054未知列“companys.name”in“where子句”谢谢您的帮助提示,我可以看到您注释掉了$query->joinWith(['rCompany')代码>-这就是列未知的原因。我在公司和策略之间没有直接关系,因此我面临以下错误:SQLSTATE[42S22]:找不到列:1054未知列“policies.companyid”在“on子句”中companyid是在策略模型中计算的
$query->andFilterWhere(['like', 'companies.name', $this->companyName]);