This article describes the YII2 implementation of the method that enables the associated fields to support the search function. Share to everyone for your reference, as follows:
Here are two tables, table structure as follows, companies_compay_id as 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 table above, you can use Companiescompany.company_name to get the company name, but this does not support searching.
To support the search function, you need to add the following code in the index view of the branches:
<?= gridview::widget ([ ' dataprovider ' = $dataProvider, ' filtermodel ' and ' = ' $searchModel, ' Columns ' = ' = [' class ' = ' Yii\grid\serialcolumn '], //Add the code to start [ ' label ' = ' Company name ', ' Attribute ' = ' companies_company_id ', ' value ' = ' companiescompany.company_name ' ], //Add the end of the code ' Companiescompany.company_name ', //' branch_id ', //' companies_company_id ', ' branch_name ', ' branch_address ', ' branch_created_date ', //' Branch_status ', [' class ' = ' yii\grid\ ' Actioncolumn '], [],]);?>
Then modify the searchbranches.php
Modify the Rules method to:
Public Function Rules () { return [[ ' branch_id '], ' integer ', [ [' Branch_name ', ' branch_address ', ' Branch_created_date ', ' branch_status ', ' companies_company_id '], ' safe '], ];}
To modify the 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 don't want to any records when validation fails $query->where (' 0=1 '); return $dataProvider; }//Add the following line of code $query->joinwith (' Companiescompany '); $query->andfilterwhere ([' branch_id ' = = $this->branch_id,//' companies_company_id ' + = $this->companie s_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])//Add Next Face this line of code->andfilterwhere ([' Like ', ' companies.company_name ', $this->companies_company_id]); return $dataProvider;
Refresh the page to see
For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on YII framework.