Implementation Code of the Yii2.0 widget GridView (two-table joint query/search/paging) function, yii2.0gridview
Join query/search/paging of two tables in the GridView
When we display active data in a grid view, you may encounter this situation, that is, displaying the values of the columns in the joined table. In order to make the joined columns sorted, you need to connect the relational table and add the sorting rules to the sorting component of the data provider to search and sort the data.
I. Controller layer Controller
<? Php namespace backend \ controllers; header ("Content-type: text/html; charset = UTF-8"); use Yii; use yii \ web \ Controller; // super controller class use backend \ models \ BooksInfo; // table Model class use backend \ models \ InfoSearch; // introduces the search Model class use yii \ data \ ActiveDataProvider; // widget data source class use yii \ grid \ GridView; // query the widget/*** @ abstract BooksController * @ author NING <[email ning@163.com]> * @ version [version 1.0] [Book Management] */class BooksInfoController Extends Controller {// book list public function actionIndex () {$ searchModel = new InfoSearch (); // instantiate searchModel [search Model] if (! Empty ($ _ GET ['infosearch']) {$ getSearch = Yii: $ app-> request-> get (); // receives the search field $ data = $ searchModel-> search ($ getSearch );} else {// widget query data $ data = new ActiveDataProvider (['query' => BooksInfo: find (), // query data 'pagination' => ['pagesize' => 2, // number of entries displayed on each page], 'sort '=> ['defaultorder' => [// 'created _ at' => SORT_DESC, 'id' => SORT_ASC, // [field] Set sorting ·],]);} // send query data, search Model return $ this-> render ('index ', ['Data' => $ data, 'searchmodel' => $ searchModel]);}?>
Ii. query Model layer models
<? Php namespace backend \ models; use Yii; use yii \ db \ ActiveRecord; /*** @ abstract [BookForm] * @ author NING <[email ning@163.com]> * @ version [vector 1.0] [book details model] */class BooksInfo extends ActiveRecord {/* ** @ set table name */public static function tableName () {return '{{% books_info }}';} // associate table public function getBooksType () {// hasOne requires that two parameters be returned. The first parameter is the class name of the joined table, and the second parameter is the association relationship between the two tables. // The id here is the id of the books_type table, associate typ of the books_info table E_id return $ this-> hasOne (BooksType: className (), ['id' => 'Type _ id']);} public function attributeLabels () {return ['id' => 'id', 'book _ name' => 'book name', 'book _ face' => 'book cover ', 'Type _ id' => 'book category id', 'Type _ name' => 'book category',] ;}}?>
Iii. Search model layer Search
<? Phpnamespace backend \ models; // namespace use Yii; use yii \ base \ Model; // introduce the base class Modeluse yii \ data \ ActiveDataProvider; // introduce the data source class/*** @ abstract [search Model] * @ return [type] * @ author NING <[email ning@163.com]> * // note: here we inherit the query Model ---> BooksInfoclass InfoSearch extends BooksInfo {public $ type_name; // defines attribute variables // only available in rules () the fields declared in the function can be searched for public function rules () {return [// [['book _ name', 'Type _ name'], 'safe '], [['typ E_name'], 'safe '],];} public function scenarios () {// The return Model: scenarios () function implemented by the bypass in the parent class ();} public function search ($ params) {$ query = BooksInfo: find (); $ dataProvider = new ActiveDataProvider (['query' => $ query, 'pagination' => ['pagesize' => 1,],]);/* The articlecategory here is the name of the method associated with the article model, except the first letter, otherwise, an error */$ query-> joinWith (['bookstype ']) will be reported. // load the filter conditions from the parameter data and verify if (! ($ This-> load ($ params) & $ this-> validate () {return $ dataProvider ;} // Add a filter condition to adjust the query object $ query-> andFilterWhere (['like', 'book _ name', $ this-> book_name]); // Add the join field filter condition. [Note: books_type in books_type.type_name is the name of the category table] $ query-> andFilterWhere (['like', 'books _ type. type_name ', $ this-> type_name]); return $ dataProvider ;}}?>
Iv. View
<? Php use yii \ grid \ GridView; use yii \ data \ ActiveDataProvider; use yii \ grid \ ActionColumn; use yii \ helpers \ Html; $ this-> title = 'book list';?> <! -- Breadcrumb --> <ol class = "breadcrumb"> <li> <a href = "#" rel = "external nofollow" = "external nofollow"> Home </a> </li> <a href = "#" rel = "external nofollow" rel = "external nofollow"> Book Information </a> </li> <li class = "active"> book list </li> </ol> <? Phpecho GridView: widget (['dataprovider' => $ data, // data source 'filtermodel' => $ searchModel, // search for the column 'columns' => [// ['filtermodel' => $ searchModel], ['class' => 'yii \ grid \ checkboxcolumn'], // check box column ['attribute' => 'id'], ['attribute' => 'book _ name',], ['attribute' => 'book _ face', 'content' => function ($ model) {// return Html: img ($ model-> book_face, ['width' => '50']);}], ['attribute' => 'Type _ name', 'v Alue '=> 'bookstype. type_name ', // two-table join query [book type], ['class' => 'yii \ grid \ ActionColumn', 'header' => 'operation'], // action column], 'pager' => [// customize the paging style and display content 'prevpagelabel '=> 'previous page', 'nextpagelabel' => 'Next page ', 'firstpagelabel '=> 'first page', 'lastpagelabel' => 'last page', 'options' => ['style' => 'margin-left: 200px; ', 'class' => "pagination"],],]);?>
V. effect display
Summary
The above is the implementation code of the Yii2.0 widget GridView (two-table joint query/search/paging) function introduced by xiaobian. I hope it will be helpful to you, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!