Home  >  Article  >  Backend Development  >  Detailed explanation of examples of search and sorting methods for CGridView association tables in Yii, yiicgridview_PHP tutorial

Detailed explanation of examples of search and sorting methods for CGridView association tables in Yii, yiicgridview_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:22730browse

Detailed explanation of examples of search and sorting methods for CGridView association tables in Yii, yiicgridview

The example in this article describes the search and sorting method of CGridView association table in Yii. Share it with everyone for your reference. The specific implementation method is as follows:

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

First of all, check protectedmodelsComment.php in your blog demo to make sure the Comment model has a search method. If not, use gii to generate one. There is no search method in the blog demo I downloaded.

Then, it’s time to write code. We start with CommentController and we add an actionList to it:

Copy code 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 doesn’t look like much, it’s the same as the crud code you generated with gii. Now let me create the view, create list.php in the /protected/views/comment/ directory and paste the following code

Copy code The code is as follows:
breadcrumbs=array(
'Comments',
);
?>

Manage Comments



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’s ‘content’, ‘status’ and ‘author’, and the post’s title. Suppose we want to add a column of article titles to this list, we only need to add post.title:

Copy code The code is as follows:
'columns'=>array(
'content',
‘post.title’,
'status',
'author',
),

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

Question:

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

Solution:

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

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

Copy code The code is as follows:
public function rules()
{
// NOTE: you should 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, what needs to be changed most is our search function. First we need to add a criteria:

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

Copy code 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 full 'tablename'.'columnname' syntax. The reason I do this is to avoid mysql throwing a 'column is ambigious error'.

In order for this to work properly, we must pass a CSort instance and a CDbCriteria instance to the CActiveDataProvider:

Copy code The code is 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 we want to display in the CGridView:

Copy code The code is as follows:
'columns'=>array(
'content',
'postTitle',
'status',
'author',
),

Refresh it and it should be fine. The effect is as shown below:

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920975.htmlTechArticleDetailed example of the search and sorting method of CGridView association table in Yii, yiicgridview This article describes the search and sorting method of CGridView association table in Yii. . Share it with everyone for your reference. Specific implementation method...
Statement:
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