search
Homephp教程PHP开发Yii2 implements methods to enable related fields to support search functions

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:

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

Then modify SearchBranches.php

Modify the rules method as:

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

Modify the search method:

public function search($params)
{
  $query = Branches::find();
  $dataProvider = new ActiveDataProvider([
    &#39;query&#39; => $query,
  ]);
  $this->load($params);
  if (!$this->validate()) {
    // uncomment the following line if you do not want to any records when validation fails
    // $query->where(&#39;0=1&#39;);
    return $dataProvider;
  }
  // 添加下面这行代码
  $query->joinWith(&#39;companiesCompany&#39;);
  $query->andFilterWhere([
    &#39;branch_id&#39; => $this->branch_id,
  //  &#39;companies_company_id&#39; => $this->companies_company_id,
    &#39;branch_created_date&#39; => $this->branch_created_date,
  ]);
  $query->andFilterWhere([&#39;like&#39;, &#39;branch_name&#39;, $this->branch_name])
    ->andFilterWhere([&#39;like&#39;, &#39;branch_address&#39;, $this->branch_address])
    ->andFilterWhere([&#39;like&#39;, &#39;branch_status&#39;, $this->branch_status])
     // 添加下面这行代码
    ->andFilterWhere([&#39;like&#39;, &#39;companies.company_name&#39;, $this->companies_company_id]);
  return $dataProvider;

Refresh the page to see

For more Yii2 methods to implement related fields to support search functions, please pay attention to the PHP Chinese website for related articles!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.