General
-
(void) init() ?
basically all classes provide this method - don't forget to call the parent
- (void) __get($name)
- (void) __set($name, $value)
Active Record
Misc
-
(void) validate() ?
checking the rules
Error
-
(void) addErrors($attribute, $error) ?
add an error for an attribute (both params are strings)
-
(bool) hasErrors() ?
true when model has an error
-
(array) getErrors([$attribute]) ?
returns the errors of the model or attribute
-
(htmlString) CHtml::errorSummary($model) ?
returns the errors as html string
hooks
-
protected (before|after)(Delete|Save|Find)() ?
protected function beforeSave(){return parent::beforeSave();}
-
$this->isNewRecord ?
often used together with hooks
- protected afterConstruct()
relations
- official
- 'VarName'=>array('RelationType', 'ClassName', 'ForeignKey', ...additional options)
- BELONGS_TO?if the relationship between table A and B is one-to-many, then B belongs to A (e.g. Post belongs to User);'author'=>array(self::BELONGS_TO, 'User', 'author_id'),
- HAS_MANY?if the relationship between table A and B is one-to-many, then A has many B (e.g. User has many Post)
- HAS_ONE?this is special case of HAS_MANY where A has at most one B (e.g. User has at most one Profile)
- BELONGS_TO?if the relationship between table A and B is one-to-many, then B belongs to AMANY_MANY: 'relation_a_b' => array(self::MANY_MANY, 'B', A_B('a_id','b_id'),
'relation_b_a' => array(self::MANY_MANY, 'A', A_B('b_id','a_id'), ? - STAT?
statistical query
'commentCount'=>array(self::STAT, 'Comment', 'post_id'),
'categoryCount'=>array(self::STAT, 'Category', 'post_category(post_id, category_id)'),
- load by attribute:$model->relationName?$posts = $user->posts; $posts=$user->posts(array('condition'=>'status=1'));
- with()?preloads all from a relation
$posts=Post::model()->with('author')->findAll();
with('a', 'b') // loads both a and b relation
with('a.b') // hierarchical loading - a and relation b from a
with(array('posts'=>array('order'=>'posts.create_time ASC'),...)//additional db-stuff
$posts=Post::model()->with('comments:recently:approved')->findAll();//scopes
searching/finding
- official
-
$post=Post::model()->find($condition,$params);
?
find the first row satisfying the specified condition
$post=Post::model()->find('postID=:postID', array(':postID'=>10)); -
$post=Post::model()->findByPk($postID,$condition,$params);
?
find the row with the specified primary key
-
$post=Post::model()->findByAttributes($attributes,$condition,$params);
?
find the row with the specified attribute values
- to get a list - use findAll, findAllByPk,...
CDbCriteria
- $criteria=new CDbCriteria;
- $post=Post::model()->find($criteria);
- select: $criteria->select='title';?a list of columns to be selected for the related AR class. It defaults to '*', meaning all columns. Column names referenced in this option should be disambiguated.
- condition: $criteria->condition='postID=:postID';?the WHERE clause. It defaults to empty. Column names referenced in this option should be disambiguated.
- params: $criteria->params=array(':postID'=>10); ?the parameters to be bound to the generated SQL statement. This should be given as an array of name-value pairs. This option has been available since version 1.0.3.
- on: ?the ON clause. The condition specified here will be appended to the joining condition using the AND operator. Column names referenced in this option should be disambiguated. This option does not apply to MANY_MANY relations. This option has been available since version 1.0.2.
- order: ?the ORDER BY clause. It defaults to empty. Column names referenced in this option should be disambiguated.
- with: ?a list of child related objects that should be loaded together with this object. Be aware that using this option inappropriately may form an infinite relation loop.
- joinType: ?type of join for this relationship. It defaults to LEFT OUTER JOIN.
- alias: ?the alias for the table associated with this relationship. This option has been available since version 1.0.1. It defaults to null, meaning the table alias is the same as the relation name.
- together: ?whether the table associated with this relationship should be forced to join together with the primary table and other tables. This option is only meaningful for HAS_MANY and MANY_MANY relations. If this option is set false, the table associated with the HAS_MANY or MANY_MANY relation will be joined with the primary table in a separate SQL query, which may improve the overall query performance since less duplicated data is returned. The default value is true. For more details, see the section "Relational Query Performance". This option has been available since version 1.0.3.
- join: ?the extra JOIN clause. It defaults to empty. This option has been available since version 1.1.3.
- group: ?
- having: ?the HAVING clause. It defaults to empty. Column names referenced in this option should be disambiguated. Note: option has been available since version 1.0.1.
- index: ?the name of the column whose values should be used as keys of the array that stores related objects. Without setting this option, an related object array would use zero-based integer index. This option can only be set for HAS_MANY and MANY_MANY relations. This option has been available since version 1.0.7.
- limit: ?limit of the rows to be selected. This option does NOT apply to BELONGS_TO relation.
- offset: ?offset of the rows to be selected. This option does NOT apply to BELONGS_TO relation.
the GROUP BY clause. It defaults to empty. Column names referenced in this option should be disambiguated.
scopes
-
used to abbreviate dbcriterias?
public function scopes() { return array( 'published'=>array( 'condition'=>'status=1', ), 'recently'=>array( 'order'=>'create_time DESC', 'limit'=>5, ), ); } $posts=Post::model()->published()->recently()->findAll();
Exceptions
- official
- throw new CHttpException(404,'The specified post cannot be found.');
- throw new CException('The specified post cannot be found.'); Yii::log($message, $level, $category); Yii::trace($message, $category); trace: this is the level used by Yii::trace. It is for tracing the execution flow of the application during development. info: this is for logging general information. profile: this is for performance profile which is to be described shortly. warning: this is for warning messages. error: this is for fatal error messages
Controller
hooks
-
protected (before|after)(Action)($action) ?
protected function beforeAction($action){if ($action->id != 'notImplemented') return parent::beforeAction($action);}
-
$this->isNewRecord ?
often used together with hooks