Yii quick learning tutorial ------ model usage (2)
When it comes to model, we may say, what do we mean by this? In fact, it is not the case that we generally know about increment. delete. change. check this field. there are some classic things for our reference and learning, ar mode and field filtering. of course, I only want to talk about some common ones.
I. Word-breaking filtering. The following example is provided. Generally, it is enough.
1.
Public Function rules ()
{
// Note: You shocould only define rules for those attributes that
// Will receive user inputs.
Return array (
Array ('vocabulary _ id, is_deleted', 'Numerical ', 'integeronly' => true ),
Array ('title', 'length', 'encoding' => 'utf-8', 'Max' => 20 ),
Array ('copyright _ owner', 'length', 'encoding' => 'utf-8', 'Max' => 50 ),
Array ('region _ code, term_id ', 'length', 'encoding' => 'utf-8', 'Max' => 10 ),
Array ('user _ id, year, month, day, view_count, rating, last_updated_by ', 'length', 'max' => 11 ),
Array ('approve _ status', 'length', 'max '=> 8 ),
Array ('additational ', 'length', 'encoding' => 'utf-8', 'Max' => 500)
);
}
Note: 'encoding' => 'utf-8' is sometimes useful. Because the width of Chinese and English characters is different, you may set the length to 100 characters, the English maximum length is indeed 100, and
A Chinese character can contain up to 50 characters, which is caused by different encodings.
2.
Public Function rules ()
{
// Note: You shocould only define rules for those attributes that
// Will receive user inputs.
Return array (
Array ('nickname, password, email, type', 'required', 'on' => 'register, create '),
Array ('nickname, email ', 'unique', 'on' => 'register, create '),
Array ('email ', 'email '),
Array ('is _ deleted', 'Numerical ', 'integeronly' => true ),
Array ('repassword', 'company', 'companyattribute '=> 'Password', 'message' => 'inconsistent password', 'on' => 'register, create, update '),
Array ('oldpassword, newpassword', 'requestred', 'on' => 'modify '),
Array ('newpassword', 'length', 'Max' => 15, 'Min' => '6', 'on' => 'modify ', 'toolong' => '{attribute} is too long (the maximum value is {max} characters ). ', 'tooshort' => '{attribute} is too short (the minimum value is {min} characters ). '),
Array ('oldpassword', 'validateoldpassword', 'on' => 'modify '),
Array ('newpassword', 'validatenewpassword', 'on' => 'modify '),
Array ('renewpassword', 'company', 'companyattribute '=> 'newpassword', 'message' => 'Confirm that the password and new password must be consistent ', 'on' => 'modify '),
Array ('repassword, newpassword, renewpassword', 'safe '),
);
}
Ii. Table connection query.
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 (
'Projecvideos '=> array (SELF: belongs_to, 'projecvideo', 'id '),
);
}
// Connection Query
Public Function getvideosprogramid ($ programid = NULL)
{
$ Criteria = new cdbcriteria;
$ Criteria-> with = 'projecvideos ';
$ Criteria-> addcondition ("T. is_deleted = '0'"); // 0 indicates that it is not deleted.
$ Criteria-> addcondition ("programvideos. is_deleted = '0'"); // 0 indicates that it is not deleted.
$ Criteria-> addcondition ('programvideos. program_id = '. (INT) $ programid );
$ Criteria-> order ='t. last_updated_time DESC ';
Return video: Model ()-> findall ($ criteria );
}
Call:
$ Video = new video ();
$ Result = $ video-> getvideosprogramid ();
Echo $ result [0]-> title;
Echo $ result [0]-> programvideos-> program_id;
Note: defining table connections is very simple. associate. to distinguish the same fields between two tables, the AR mode provides a table alias t to represent the table name corresponding to the current model.
This avoids conflicts.
Reprinted: http://hi.baidu.com/itrunner/item/17d474dd82f6611ee0f46f13