1. Introduction
It has been almost two months since contact with the YII framework, but its understanding of the YII framework is not very deep, and has not been systematically studied, only to do the project when it is not know knowledge to go through the manual.
In the previous project because of the need to display the fields of the associated table and with the search sort function, this has not been contacted before, so in the manual to find the relevant information to write this demand, and in the Youdao cloud made some notes, today is just the weekend time to organize it into a blog it.
Don't say much nonsense, go straight to the steps.
2. Operation procedure
Background: This adds the associated table fields to the GII auto-generated curl.
Requirement: A table has a field pt_id associated B table; Now, on the basis of curl based on the A-table gii, we need to increase the name value of the table field of type A, with the search sort function.
2.1. Step1 add a method to the A model
1//meaning to get PT Data 2 Public Function getpt () //get** get back any 3 {4 //model name: ClassName (), B table field =>a tables corresponding fields 5 return $this->hasone (Pt::classname (), [' id ' = ' ptid ']); 6}
2.2. step2 add field variables to be associated in a search
1 public $name; Can be associated to display multiple fields 2 public $*****; Example
2.3. Modify the search function in a search
1//search function adds an associative query added in the SQL Statement 2//looks so familiar, haha 3 $query->joinwith ([' PT ']); Wait here the value in PT is the name after get in Step1
2.4. Set the sort configuration in a search
1//Here are the fields that can be sorted 2//If a search already exists setsort, then other move, only need to add a new field to 3//However, my Yii 2 generated search does not exist this sort, so you need to add additional 4//NOTE: When this configuration is not present in your search, it indicates that the default configuration is set up, and when you add a setsort here, the default configuration will be overwritten, and the field with the sort function previously will be invalidated by 5 $dataProvider Setsort ( 6 ' attributes ' =>[ 7//==== Add new field sort configuration = 8 ' name ' =>[ 9 ' ASC ' =>[' name ' = = Sort_asc], "Desc ' =>[' name ' = ' = Sort_desc], one ' lable ' + ' name ', 12], 13//================== 14] 15);
2.5. Add filter configuration in a search
1//In the code to add this sentence in the formation of 2//like, the associated table full name, This->name3 $query->andfilterwhere ([' Like ', ' **_pt.name ', $this->name]) ;
2.6. Modify the GridView in the Index view
1 ' Columns ' = [ 2//=========== 3//Add 4 [ 5 ' attribute ' = ' name ', 6 ' label ' = ' Platform ', 7 ' value ' = ' pt.name ', 8 ' filter ' = Html::activetextinput ($searchModel, ' name ', [ 9 ' class ' = ' Form-control ' ), 11], 12//============= 13]
At this time has finished more than half, you visit this controller, you can already see the page effect, but in the search time does not work, why?
Because the field information you submitted is not secure at this time, there is no record in the program.
2.7. Set the field to the security level
1//Add 2//name fields to the set rule method in a search safe3 [[' Name '], safe]
Gaocheng!!
3. Summary
Here is just a small example, through this example can know how to add related fields Yii method, then you can according to the existing knowledge to change more patterns out ....