An example of Cgridview correlation table Search Sort method in Yii _php example

Source: Internet
Author: User
Tags yii

The example of this article describes the search sort method of Cgridview association table in Yii. Share to everyone for your reference. The implementation methods are as follows:

The implementation method of search sort in Yii Cgridview Association table A little complicated, today I saw a foreigner wrote a game, I would like to tidy up and share with you friends, I believe it will be helpful to the study of YII framework.

First, check your blog demo protectedmodelscomment.php to ensure that the comment model has a search method, if not, use the GII to generate a, I downloaded to the blog demo is not.

And then, it's time to write code, we start with Commentcontroller, we add a actionlist to it:

Copy Code code 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 doesn't look that great, as in the CRUD code you've generated in GII. Now let me create the view, create the list.php in the/protected/views/comment/directory, and then paste the following code

Copy Code code as follows:
<?php $this->breadcrumbs=array (
' 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 comments ' content ', ' status ' and ' author ', and the title of the article. Let's say we want to add the title of a column to this list, we just need to add Post.title:

Copy Code code as follows:
' Columns ' =>array (
' Content ',
' Post.title ',
' Status ',
' Author ',
),

Now if you visit the following page, you find that the title of the article does show up

Problem:

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

Solution:

In order to solve this problem, we need to take some effort. First we have to add a getter and a setter to the commen model, for example:

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

Copy Code code as follows:
Public Function Rules ()
{
Note:you should only define the rules for those attributes
would 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 need to change is our search function. First we need to add a criteria:

Copy Code code as follows:
$criteria =new Cdbcriteria;
$criteria->with = "POST"; Make sure the post table is queried

$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 sort:

Copy Code code 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'm using the full ' tablename '. ColumnName ' syntax, the reason I do this is to avoid MySQL throwing ' column is ambigious error '.

To ensure that all this works, we must pass Csort instances and Cdbcriteria instances to Cactivedataprovider:

Copy Code code as follows:
return new Cactivedataprovider (' Comment ', Array (
' Criteria ' => $criteria,
' Sort ' => $sort
));

return new Cactivedataprovider (' Comment ', Array (
' Criteria ' => $criteria,
' Sort ' => $sort
));

Now all we have to do is modify our view so that it displays the properties you want to display in Cgridview:

Copy Code code as follows:
' Columns ' =>array (
' Content ',
' Posttitle ',
' Status ',
' Author ',
),

Refresh, should be able to, the effect shown in the following figure:

I hope this article will help you with the PHP program design 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.