diff --git a/framework/YiiBase.php b/framework/YiiBase.php index e6f5a00..39b669e 100644 --- a/framework/YiiBase.php +++ b/framework/YiiBase.php @@ -380,7 +380,7 @@ class YiiBase $object->$key = $value; } - if ($object instanceof \yii\base\Component) { + if ($object instanceof \yii\base\Initable) { $object->init(); } diff --git a/framework/base/Component.php b/framework/base/Component.php index 0e0fe81..ede1c50 100644 --- a/framework/base/Component.php +++ b/framework/base/Component.php @@ -332,7 +332,7 @@ class Component * ) * ~~~ * - * Note that the behavior classes must extend from [[Behavior]]. Behaviors declared + * Note that a behavior class must extend from [[Behavior]]. Behaviors declared * in this method will be attached to the model when [[init]] is invoked. * * @return array the behavior configurations. diff --git a/framework/base/Initable.php b/framework/base/Initable.php new file mode 100644 index 0000000..90bc769 --- /dev/null +++ b/framework/base/Initable.php @@ -0,0 +1,36 @@ + + * @since 2.0 + */ +interface Initable +{ + /** + * Initializes this component. + * This method is invoked by [[\Yii::createComponent]] after its creates the new + * component instance and initializes the component properties. In other words, + * at this stage, the component has been fully configured. + * + * The default implementation calls [[behaviors]] and registers any available behaviors. + * You may override this method with additional initialization logic (e.g. establish DB connection). + * Make sure you call the parent implementation. + */ + public function init(); +} diff --git a/framework/base/Model.php b/framework/base/Model.php index 3b29ae4..918a9df 100644 --- a/framework/base/Model.php +++ b/framework/base/Model.php @@ -23,7 +23,6 @@ namespace yii\base; * Model also provides a set of events for further customization: * * - [[onAfterConstruct]]: an event raised at the end of constructor - * - [[onInit]]: an event raised when [[init]] is called * - [[onBeforeValidate]]: an event raised at the beginning of [[validate]] * - [[onAfterValidate]]: an event raised at the end of [[validate]] * @@ -33,7 +32,7 @@ namespace yii\base; * @author Qiang Xue * @since 2.0 */ -class Model extends Component implements \IteratorAggregate, \ArrayAccess +class Model extends Component implements Initable, \IteratorAggregate, \ArrayAccess { private static $_attributes = array(); // class name => array of attribute names private $_errors; // attribute name => array of errors @@ -51,6 +50,46 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess } /** + * Initializes this model. + * + * This method is required by the [[Initable]] interface. It is invoked by [[\Yii::createComponent]] + * after its creates the new model instance and initializes the model properties. + * + * The default implementation calls [[behaviors]] and registers any available behaviors. + * You may override this method with additional initialization logic (e.g. establish DB connection). + * Make sure you call the parent implementation. + */ + public function init() + { + $this->attachBehaviors($this->behaviors()); + } + + /** + * Returns a list of behaviors that this model should behave as. + * The return value should be an array of behavior configurations indexed by + * behavior names. Each behavior configuration can be either a string specifying + * the behavior class or an array of the following structure: + * + * ~~~ + * 'behaviorName' => array( + * 'class' => 'BehaviorClass', + * 'property1' => 'value1', + * 'property2' => 'value2', + * ) + * ~~~ + * + * Note that a behavior class must extend from [[Behavior]]. Behaviors declared + * in this method will be attached to the model when [[init]] is invoked. + * + * @return array the behavior configurations. + * @see init + */ + public function behaviors() + { + return array(); + } + + /** * Returns the list of attribute names. * By default, this method returns all public non-static properties of the class. * You may override this method to change the default. @@ -156,19 +195,6 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess } /** - * Initializes the model. - * The default implementation raises the [[onInit]] event. - * If you override this method, make sure you call the parent implementation. - */ - public function init() - { - parent::init(); - if ($this->hasEventHandlers('onInit')) { - $this->onInit(new Event($this)); - } - } - - /** * Performs the data validation. * * This method executes the validation rules as declared in [[rules]]. diff --git a/framework/base/ModelBehavior.php b/framework/base/ModelBehavior.php index f8edd13..f8fa23b 100644 --- a/framework/base/ModelBehavior.php +++ b/framework/base/ModelBehavior.php @@ -25,7 +25,6 @@ class ModelBehavior extends Behavior * The default implementation returns the following event handlers: * * - `onAfterConstruct` event: [[afterConstruct]] - * - `onInit` event: [[initModel]] * - `onBeforeValidate` event: [[beforeValidate]] * - `onAfterValidate` event: [[afterValidate]] * @@ -36,22 +35,12 @@ class ModelBehavior extends Behavior { return array( 'onAfterConstruct' => 'afterConstruct', - 'onInit' => 'initModel', 'onBeforeValidate' => 'beforeValidate', 'onAfterValidate' => 'afterValidate', ); } /** - * Responds to [[Model::onInit]] event. - * Overrides this method if you want to handle the corresponding event of the [[owner]]. - * @param Event $event event parameter - */ - public function initModel($event) - { - } - - /** * Responds to [[Model::onAfterConstruct]] event. * Overrides this method if you want to handle the corresponding event of the [[owner]]. * @param Event $event event parameter diff --git a/todo.txt b/todo.txt index f6e5dc0..f9d7329 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,9 @@ - base - * add more doc to Model * error/exception handling + * security + * module + * application + * http exception - validators * CompareValidator::clientValidateAttribute(): search for "CHtml::activeId" * FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD