Created
September 18, 2012 23:40
-
-
Save mailopl/3746749 to your computer and use it in GitHub Desktop.
In the framework jungle: Yii Basic Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User extends CActiveRecord { | |
public static function model($className=__CLASS__) | |
{ | |
return parent::model($className); | |
} | |
public function relations() | |
{ | |
return array( | |
'posts' => array(self::HAS_MANY, 'Post', 'user_id'), | |
); | |
} | |
public function rules() | |
{ | |
return array( | |
array('email, password', 'required'), | |
array('email', 'email'), | |
array('email','unique'), | |
array('password', 'authenticate'), | |
array('password','length','min'=>8,'max'=>45), | |
); | |
} | |
} | |
class Post extends CActiveRecord { | |
public function rules() | |
{ | |
return array( | |
array('name, content', 'required'), | |
array('name','length','min'=>3,'max'=>45), | |
array('content','length','min'=>3,'max'=>2000), | |
); | |
} | |
public function relations() | |
{ | |
return array( | |
'tags' => array(self::MANY_MANY, 'Tag', 'posts_tags(post_id, tag_id)'), | |
'user' => array(self::BELONGS_TO, 'User', 'user_id') | |
); | |
} | |
} | |
class Tag extends CActiveRecord { | |
public function relations() | |
{ | |
return array( | |
'posts' => array(self::MANY_MANY, 'Post', 'posts_tags(post_id, tag_id)'), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment