


Implementation method of YII related field with search and sorting function
1. Introduction
It has been almost two months since I came into contact with the yii framework, but my understanding of the yii framework is not very deep. I have not studied systematically, I am just doing it. When working on a project, I only turn to the manual when I encounter something I don’t understand.
In the previous project, it was necessary to display the fields of the associated tables and provide search and sorting functions. I had not come across this before, so I searched for relevant information in the manual and wrote out this requirement. , and took some notes on Youdao Cloud. Today happens to be the weekend and I have time to organize it into a blog.
Without further ado, let’s get straight to the steps.
2. Operation steps
Background: Here, association table fields are added to the CURL automatically generated by GII.
Requirement: There is a field pt_id in table A associated with table B; now it is necessary to add the name value of the field associated with table B based on the CURL generated by GII of table A, with a search and sorting function .
2.1. Step 1 Add a method in A model
1 //意思即获取Pt的数据 2 public function getPt() //get** get后面任意 3 { 4 //模型名:className(),B表字段=>A表对应字段 5 return $this->hasOne(Pt::className(), ['id' => 'ptid']); 6 }
2.2. Step 2 Add field variables that need to be associated in A search
1 public $name; //可以关联显示多个字段 2 public $*****; //例子
2.3. Modify the search function in A search
1 //search函数中添加 表示在SQL语句中增加关联查询 2 //看起来是那么的熟悉,哈哈 3 $query->joinWith(['pt']); //这里等待pt中的值就是step1中get后面的命名
2.4. Set the sorting configuration in A search
1 //这里存放的是可以排序的字段 2 //如果A Search已经存在setSort,则其他动,只需要添加新的字段就可以了 3 //但是,我的yii 2生成的Search不存在这个排序,因此就需要额外添加了 4 //注意:当你的search中不存在这个配置时,即表明默认配置里面有设置过,当你在这里添加一个setSort则将会覆盖默认配置,之前默认有排序功能的字段将会失效 5 $dataProvider->setSort( 6 'attributes'=>[ 7 //====添加新字段排序配置= 8 'name'=>[ 9 'asc'=>['name' => SORT_ASC], 10 'desc'=>['name' => SORT_DESC], 11 'lable'=>'name', 12 ], 13 //================== 14 ] 15 );
2.5. Add filtering configuration in A search
1 //在代码里面照着队形添加这一句即可 2 //like,关联表全名,this->name 3 $query->andFilterWhere(['like', '**_pt.name', $this->name]) ;
2.6. Modify the gridview in the index view
1 'columns' => [ 2 //=========== 3 //添加 4 [ 5 'attribute' => 'name', 6 'label' => '平台', 7 'value' => 'pt.name', 8 'filter' => Html::activeTextInput($searchModel, 'name', [ 9 'class' => 'form-control' 10 ]), 11 ], 12 //============= 13 ]
At this point, it is more than half done. When you access this controller, you can already see the page effect, but it does not work when searching. Why?
Because the field information you submitted at this time is unsafe and is not recorded in the program.
2.7. Set the security level for this field
1 //在A Search中的设置规则方法中添加 2 //name字段为safe 3 [['name'], safe]
Done! !
3. Summary
This is just a small example. Through this example, you can know how to add related fields in Yii, then you can change more based on your existing knowledge. The tricks come out. . . .
The above is the detailed content of Implementation method of YII related field with search and sorting function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

In the era dominated by intelligence, office software has also become popular, and Wps forms are adopted by the majority of office workers due to their flexibility. At work, we are required not only to learn simple form making and text entry, but also to master more operational skills in order to complete the tasks in actual work. Reports with data and using forms are more convenient, clear and accurate. The lesson we bring to you today is: The WPS table cannot find the data you are searching for. Why please check the search option location? 1. First select the Excel table and double-click to open it. Then in this interface, select all cells. 2. Then in this interface, click the "Edit" option in "File" in the top toolbar. 3. Secondly, in this interface, click "

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

The mobile Taobao app software provides a lot of good products. You can buy them anytime and anywhere, and everything is genuine. The price tag of each product is clear. There are no complicated operations at all, making you enjoy more convenient shopping. . You can search and purchase freely as you like. The product sections of different categories are all open. Add your personal delivery address and contact number to facilitate the courier company to contact you, and check the latest logistics trends in real time. Then some new users are using it for the first time. If you don’t know how to search for products, of course you only need to enter keywords in the search bar to find all the product results. You can’t stop shopping freely. Now the editor will provide detailed online methods for mobile Taobao users to search for store names. 1. First open the Taobao app on your mobile phone,

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

In our work, we often use wps software. There are many ways to process data in wps software, and the functions are also very powerful. We often use functions to find averages, summaries, etc. It can be said that as long as The methods that can be used for statistical data have been prepared for everyone in the WPS software library. Below we will introduce the steps of how to sort the scores in WPS. After reading this, you can learn from the experience. 1. First open the table that needs to be ranked. As shown below. 2. Then enter the formula =rank(B2, B2: B5, 0), and be sure to enter 0. As shown below. 3. After entering the formula, press the F4 key on the computer keyboard. This step is to change the relative reference into an absolute reference.

PHP is a server-side scripting language widely used in web development. Its main function is to generate dynamic web content. When combined with HTML, it can create rich and colorful web pages. PHP is powerful. It can perform various database operations, file operations, form processing and other tasks, providing powerful interactivity and functionality for websites. In the following articles, we will further explore the role and functions of PHP, with detailed code examples. First, let’s take a look at a common use of PHP: dynamic web page generation: P
