Skip to content

Instantly share code, notes, and snippets.

@mailopl
Created September 18, 2012 23:40
Show Gist options
  • Save mailopl/3746749 to your computer and use it in GitHub Desktop.
Save mailopl/3746749 to your computer and use it in GitHub Desktop.
In the framework jungle: Yii Basic Model
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