Yii CGridView basic use (3) associated table related field search, yiicgridview

Source: Internet
Author: User

Yii CGridView basic use (3) associated table related field search, yiicgridview

Search related fields in the joined table:

First, we will only talk about "one-to-many" association search here. First, do not forget our database. If you forget it, please stamp it here: Here, we can see that there is a foreign key associated with the tbl_user table in tbl_post to find information about the author. After the database is created, check the POST Model of the Yii code we generated. The realtion is as follows (ignore comment ):

/** * @return array relational rules. */public function relations(){// NOTE: you may need to adjust the relation name and the related// class name for the relations automatically generated below.return array('comments' => array(self::HAS_MANY, 'Comment', 'post_id'),'author' => array(self::BELONGS_TO, 'User', 'author_id'),);}
You can see that the POST and USER tables can be accessed through the author key, for example, $ model-> author-> nickname, and here is the BELONGS_TO relationship.

After talking about this, what are our requirements ?....


The product manager pushed the glasses: "We need to add a function on the log Background Management Interface. You can search for the corresponding articles by the author name. This is urgent and will be completed tonight. "


Don't you just change your needs. Ignore the progress requirement. Let's look at what to do.

In fact, it is very simple, not to add a column of author names in the POST admin interface, and then you can find the corresponding log through the fuzzy search of the author name? Look at the code. Isn't it easy to search by Author id? But it's really unfriendly... Isn't it easy to show the author's name? Add a header and set the value to $ data-> author-> username. The problem is that the header can only be displayed, but cannot be searched.

Not multiple searches? Come on, let me tell you how to do it.


First, we enter the POST model and add an attribute at the beginning:

Class Post extends CActiveRecord {public $ name; // Add a public attribute to indicate the author's name.
Then modify the search code in the Model. The changes have been added with notes:

Public function search () {// @ todo Please modify the following code to remove attributes that shocould not be searched. $ criteria = new CDbCriteria; $ criteria-> with = array ('author '); // added and author eager loading $ criteria-> compare ('Post _ id', $ this-> post_id); $ criteria-> compare ('title ', $ this-> title, true); $ criteria-> compare ('content', $ this-> content, true); $ criteria-> compare ('tags ', $ this-> tags, true); $ criteria-> compare ('status', $ this-> status); $ criteria-> compare ('create _ time ', $ this-> create_time); $ criteria-> compare ('Update _ time', $ this-> update_time); $ criteria-> compare ('author _ id ', $ this-> author_id); // here a compare is added, username is the field of the User table, $ this-> name is the attribute we added, true: Fuzzy search $ criteria-> compare ('username', $ this-> name, true); return new CActiveDataProvider ($ this, array ('Criteria '=> $ criteria ,));}


In the view, the admin. php file in the post folder is changed to the following code:

<? Php $ this-> widget ('zii. widgets. grid. CGridView ', array ('id' => 'post-grid', 'dataprovider' => $ model-> search (), 'filter' => $ model, 'columns '=> array ('Post _ id', 'title', 'content', 'tags', 'status', 'create _ time ', 'Update _ time', 'author _ id',/* is the added code */array ('name' => 'author name ', 'value' => '$ data-> author-> username', // define the value 'filter' => CHtml: activeTextField ($ model, 'name'), // Add search filter), array ('class' => 'cbutto NColumn ',);?>
Did you find that the search box does not work now? Haha, so we say that we should stick to seeing the end of the article. The last step is to add the name attribute to the security search field in the rule. Otherwise, the attribute will be filtered out by Yii as an insecure field. Look, in the last row of the following function, there is a name in front of safe ....

public function rules(){// NOTE: you should only define rules for those attributes that// will receive user inputs.return array(array('title, content, status, author_id', 'required'),array('status, create_time, update_time, author_id', 'numerical', 'integerOnly'=>true),array('title', 'length', 'max'=>128),array('tags', 'safe'),// The following rule is used by search().// @todo Please remove those attributes that should not be searched.array('post_id, title, content, tags, status, create_time, update_time, author_id, name', 'safe', 'on'=>'search'),);}


This function has been completed ~~ Where do I need to wait till night ~~ Don't tell the product manager. Let's get started with a storm .....

(Well, I admit that I wrote this Well Because Wen Qing suffered another illness and went to the treatment stage ....)


 


Yii CGridView click Event

Array (
'Header' => 'view audience ',
//....
'Buttons' => array (
'Play' => array (
'Label' => 'player ',
'Imageurl' => Yii: app ()-> request-> baseUrl. '/images/play.jpg ',
'Url' => 'yii: app ()-> createUrl ("/yin/play", array ("wavpath" => $ data-> wav_path ))',
'Click' => 'noborderwin (this. src, 300,200, "#000000", "# CCCCCC", "audio playback", "#000000", "no"); return false ;',
),
)
);
Gridview parses $ data references in real time (executed using eval (). Therefore, it must be stored as a string (therefore, strings must be enclosed in single quotes). Writing directly will certainly report that the variable is undefined.
Hope to help you
Reference: www.yiiframework.com/doc/api/1.1/CGridView

CGridView in yii framework

Add sort Field

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.