Home >Backend Development >PHP Tutorial >Yii2 implements methods to enable associated fields to support search functions_php example

Yii2 implements methods to enable associated fields to support search functions_php example

WBOY
WBOYOriginal
2016-08-17 13:02:30857browse

The example in this article describes how Yii2 implements the search function for related fields. Share it with everyone for your reference, the details are as follows:

There are two tables here. The table structure is as follows, companies_compay_id is the foreign key

yii2advanced.branches table:

branch_id:int(11)
companies_company_id:int(11)
branch_name:varchar(100)
branch_address:varchar(255)
branch_created_date:datetime
branch_status:enum('active','inactive')

yii2advanced.companies table:

company_id:int(11)
company_name:varchar(100)
company_email:varchar(100)
company_address:varchar(255)
logo:varchar(200)
company_start_date:datetime
company_create_date:datetime
company_status:enum('active','inactive')

In the above table, you can use companiesCompany.company_name to get the company name, but this does not support search.

To support the search function, you need to add the following code to the index view of branches:

<&#63;= GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    //添加的代码开始
    [
      'label'=>'公司名',
      'attribute'=>'companies_company_id',
      'value'=>'companiesCompany.company_name'
    ],
     //添加的代码结束
    'companiesCompany.company_name',
    // 'branch_id',
    // 'companies_company_id',
    'branch_name',
    'branch_address',
    'branch_created_date',
    // 'branch_status',
    ['class' => 'yii\grid\ActionColumn'],
  ],
]); &#63;>

Then modify SearchBranches.php

The method to modify the rules is:

public function rules()
{
  return [
    [['branch_id'], 'integer'],
    [['branch_name', 'branch_address', 'branch_created_date', 'branch_status','companies_company_id'], 'safe'],
  ];
}

Modify search method:

public function search($params)
{
  $query = Branches::find();
  $dataProvider = new ActiveDataProvider([
    'query' => $query,
  ]);
  $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->joinWith('companiesCompany');
  $query->andFilterWhere([
    'branch_id' => $this->branch_id,
  //  'companies_company_id' => $this->companies_company_id,
    'branch_created_date' => $this->branch_created_date,
  ]);
  $query->andFilterWhere(['like', 'branch_name', $this->branch_name])
    ->andFilterWhere(['like', 'branch_address', $this->branch_address])
    ->andFilterWhere(['like', 'branch_status', $this->branch_status])
     // 添加下面这行代码
    ->andFilterWhere(['like', 'companies.company_name', $this->companies_company_id]);
  return $dataProvider;

Refresh the page to see it

Readers who are interested in more Yii-related content can check out the special topics of this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent Development Framework of PHP", "Basic Tutorial for Getting Started with Smarty Templates", "Introduction to PHP Object-Oriented Programming" Tutorial", "php string (string) usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will help you design PHP programs based on the Yii framework.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn