Examples of CGridView associated table search and sorting method in Yii

Source: Internet
Author: User
This article mainly introduces the CGridView associated table search and sorting method in Yii, and analyzes in detail the implementation process of the CGridView associated table search and sorting and the solution to the problems in the search results in the form of examples, is a very practical technique,

This article mainly introduces the CGridView associated table search and sorting method in Yii, and analyzes in detail the implementation process of the CGridView associated table search and sorting and the solution to the problems in the search results in the form of examples, is a very practical technique,

This article describes how to search and sort CGridView associated tables in Yii. Share it with you for your reference. The specific implementation method is as follows:

The implementation method of searching and sorting in the Yii CGridView associated table is a bit complicated. Today I read a game written by a foreigner. I will sort it out and share it with you, I believe it will be helpful for you to learn the Yii framework.

First, check protectedmodelsComment in your blog demo. php: Make sure that the Comment model has a search method. If not, use gii to generate one. The blog demo I downloaded does not exist.

Then, it's time to write the code. Starting from CommentController, we add an actionList to it:

The Code is as follows:

Public function actionList ()
{
$ Model = new Comment ('search ');
$ Model-> unsetAttributes ();
If (isset ($ _ GET ['comment'])
$ Model-> attributes = $ _ GET ['comment'];

$ This-> render ('LIST', array (
'Model' => $ model,
));
}

It seems nothing remarkable, just like in the crud code generated with gii. Now let me create a view, create list. php In the/protected/views/comment/directory, and then paste the following code

The Code is as follows:

<? Php $ this-> breadcrumbs = array (
'Comments ',
);
?>

Manage Comments

<? Php $ this-> widget ('zii. widgets. grid. CGridView ', array (
'Dataprovider' => $ model-> search (),
'Filter' => $ model,
'Columns '=> array (
'Content ',
'Post. title ',
'Status ',
'Author'
),
));
?>

Comment List

This is a basic CGridView that only displays the comment 'content', 'status' and 'author', and the article title. Suppose we want to add a column of article titles to this list, we just need to add post. title:

The Code is as follows:

'Columns '=> array (
'Content ',
'Post. title ',
'Status ',
'Author ',
),

Now, if you visit the following page and find that the title of the article is displayed

Problem:

If you look at this page carefully, you will find that you cannot search for the title of the article, and you cannot sort by the title of the article, because CGridView found a 'in the given column name '. ', that is, post. title point. If there is a number, it will not generate a search box.

Solution:

We have to work hard to solve this problem. First, we need to add a getter and a setter to The Commen model, for example:

The Code is as follows:

Private $ _ postTitle = null;
Public function getPostTitle ()
{
If ($ this-> _ postTitle === null & $ this-> post! = Null)
{
$ This-> _ postTitle = $ this-> post-> title;
}
Return $ this-> _ postTitle;
}
Public function setPostTitle ($ value)
{
$ This-> _ postTitle = $ value;
}

Next, add this attribute to the rules function:

The Code is as follows:

Public function rules ()
{
// NOTE: you shocould only define rules for those attributes that
// Will receive user inputs.
Return array (
Array ('content, author, email ', 'required '),
Array ('author, email, url', 'length', 'max '=> 128 ),
Array ('email ', 'email '),
Array ('url', 'url ')

Array ('content, postTitle, status, author', 'safe ', 'on' => 'search '),
);
}

This is not enough. The most important thing to change is our search function. First, we need to add a criteria:

The Code is as follows:

$ Criteria = new CDbCriteria;
$ Criteria-> with = "post"; // make sure to query the post table

$ Criteria-> compare ('t. content ', $ this-> content, true );
$ Criteria-> compare ('t. status ', $ this-> status );
$ Criteria-> compare ('t. author ', $ this-> author, true );
$ Criteria-> compare ('Post. title', $ this-> postTitle, true );

Then we add the sorting:

The Code is as follows:

$ Sort = new CSort ();
$ Sort-> attributes = array (
'Defaultorder' => 't. create_time DESC ',
'Content' => array (
'Asc '=>' t. content ',
'Desc' => 't. content desc ',
),
'Status' => array (
'Asc '=>' t. status ',
'Desc' => 't. status desc ',
),
'Author' => array (
'Asc '=>'t. author ',
'Desc' =>'t. author desc ',
),
'Posttitle' => array (
'Asc '=> 'Post. title ',
'Desc' => 'Post. title desc ',
),
);

You may have noticed that I am using the complete 'tablename'. 'columnname' syntax. I am doing this to prevent mysql from throwing 'column is ambigious error '.

To ensure that all the operations are normal, we must pass the CSort instance and the CDbCriteria instance to the CActiveDataProvider:

The Code is as follows:

Return new CActiveDataProvider ('comment', array (
'Criteria '=> $ criteria,
'Sort '=> $ sort
));

Return new CActiveDataProvider ('comment', array (
'Criteria '=> $ criteria,
'Sort '=> $ sort
));

Now we need to modify our view so that it can display the desired properties in the CGridView:

The Code is as follows:

'Columns '=> array (
'Content ',
'Posttitle ',
'Status ',
'Author ',
),

Refresh the page, as shown in the following figure:

I hope this article will help you design PHP programs based on the Yii framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.