Detailed description of CGridView associated table search and sorting methods in Yii

Source: Internet
Author: User
Tags comments yii

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: Copy code

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: Copy code
<? Php $ this-> breadcrumbs = array (
'Comments ',
);
?>
 
<H1> 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: Copy code
'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: Copy code

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: Copy code

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: Copy code
$ 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: Copy code
$ 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: Copy code
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: Copy code
'Columns '=> array (
'Content ',
'Posttitle ',
'Status ',
'Author ',
),


Refresh the page and you can:

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.