Table of Contents
1. Introduction
2. Operation steps
2.1. Step 1 Add a method in A model
2.2. Step 2 Add fields that need to be associated in A search Variable
This is just a small example. Through this example, you can know how to add related fields in Yii, then you can use it based on the existing Knowledge changes things in more ways. . . .
Home Backend Development PHP Tutorial Implementation case of associated fields with search and sorting function in Yii framework

Implementation case of associated fields with search and sorting function in Yii framework

Oct 23, 2017 am 09:06 AM
Field sort search

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 }
Copy after login

2.2. Step 2 Add fields that need to be associated in A search Variable


1 public $name;   //可以关联显示多个字段
2 public $*****;  //例子
Copy after login

2.3. Modify the search function in A search

1 //search函数中添加 表示在SQL语句中增加关联查询
2 //看起来是那么的熟悉,哈哈
3 $query->joinWith(['pt']);   //这里等待pt中的值就是step1中get后面的命名
Copy after login

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 );
Copy after login

2.5. Add the filtering configuration in A search

1 //在代码里面照着队形添加这一句即可
2 //like,关联表全名,this->name
3 $query->andFilterWhere(['like', '**_pt.name', $this->name]) ;
Copy after login

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 ]
Copy after login

At this point, more than half of the work is completed. You can visit In 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字段为safe3 [['name'], safe]
Copy after login

The job is done! !

3. Summary

The above is the detailed content of Implementation case of associated fields with search and sorting function in Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
How to sort photos by date taken in Windows 11/10 How to sort photos by date taken in Windows 11/10 Feb 19, 2024 pm 08:45 PM

This article will introduce how to sort pictures according to shooting date in Windows 11/10, and also discuss what to do if Windows does not sort pictures by date. In Windows systems, organizing photos properly is crucial to making it easy to find image files. Users can manage folders containing photos based on different sorting methods such as date, size, and name. In addition, you can set ascending or descending order as needed to organize files more flexibly. How to Sort Photos by Date Taken in Windows 11/10 To sort photos by date taken in Windows, follow these steps: Open Pictures, Desktop, or any folder where you place photos In the Ribbon menu, click

How to remove news and trending content from Windows 11 Search How to remove news and trending content from Windows 11 Search Oct 16, 2023 pm 08:13 PM

When you click the search field in Windows 11, the search interface automatically expands. It displays a list of recent programs on the left and web content on the right. Microsoft displays news and trending content there. Today's check promotes Bing's new DALL-E3 image generation feature, the "Chat Dragons with Bing" offer, more information about dragons, top news from the Web section, game recommendations, and the Trending Search section. The entire list of items is independent of your activity on your computer. While some users may appreciate the ability to view news, all of this is abundantly available elsewhere. Others may directly or indirectly classify it as promotion or even advertising. Microsoft uses interfaces to promote its own content,

How to sort emails by sender, subject, date, category, size in Outlook How to sort emails by sender, subject, date, category, size in Outlook Feb 19, 2024 am 10:48 AM

Outlook offers many settings and features to help you manage your work more efficiently. One of them is the sorting option that allows you to categorize your emails according to your needs. In this tutorial, we will learn how to use Outlook's sorting feature to organize emails based on criteria such as sender, subject, date, category, or size. This will make it easier for you to process and find important information, making you more productive. Microsoft Outlook is a powerful application that makes it easy to centrally manage your email and calendar schedules. You can easily send, receive, and organize email, while built-in calendar functionality makes it easy to keep track of your upcoming events and appointments. How to be in Outloo

How to use Baidu advanced search How to use Baidu advanced search Feb 22, 2024 am 11:09 AM

How to use Baidu Advanced Search Baidu search engine is currently one of the most commonly used search engines in China. It provides a wealth of search functions, one of which is advanced search. Advanced search can help users search for the information they need more accurately and improve search efficiency. So, how to use Baidu advanced search? The first step is to open the Baidu search engine homepage. First, we need to open Baidu’s official website, which is www.baidu.com. This is the entrance to Baidu search. In the second step, click the Advanced Search button. On the right side of the Baidu search box, there is

How to search for users in Xianyu How to search for users in Xianyu Feb 24, 2024 am 11:25 AM

How does Xianyu search for users? In the software Xianyu, we can directly find the users we want to communicate with in the software. But I don’t know how to search for users. Just view it among the users after searching. Next is the introduction that the editor brings to users about how to search for users. If you are interested, come and take a look! How to search for users in Xianyu? Answer: View details among the searched users. Introduction: 1. Enter the software and click on the search box. 2. Enter the user name and click Search. 3. Select [User] under the search box to find the corresponding user.

WPS table cannot find the data you are searching for, please check the search option location WPS table cannot find the data you are searching for, please check the search option location Mar 19, 2024 pm 10:13 PM

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 "

How to search for stores on mobile Taobao How to search for store names How to search for stores on mobile Taobao How to search for store names Mar 13, 2024 am 11:00 AM

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,

How does Arrays.sort() method in Java sort arrays by custom comparator? How does Arrays.sort() method in Java sort arrays by custom comparator? Nov 18, 2023 am 11:36 AM

How does Arrays.sort() method in Java sort arrays by custom comparator? In Java, the Arrays.sort() method is a very useful method for sorting arrays. By default, this method sorts in ascending order. But sometimes, we need to sort the array according to our own defined rules. At this time, you need to use a custom comparator (Comparator). A custom comparator is a class that implements the Comparator interface.

See all articles