Browse Source

w

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
b158ff074b
  1. 2
      framework/YiiBase.php
  2. 2
      framework/base/Component.php
  3. 36
      framework/base/Initable.php
  4. 56
      framework/base/Model.php
  5. 11
      framework/base/ModelBehavior.php
  6. 5
      todo.txt

2
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();
}

2
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.

36
framework/base/Initable.php

@ -0,0 +1,36 @@
<?php
/**
* Initable interface file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* Initable is an interface indicating a class needs initialization to work properly.
*
* Initable requires a class to implement the [[init]] method.
* When [[\Yii::createComponent]] is creating a new component instance, if the component
* class implements Initable interface, the method will call its [[init]] method
* after setting the initial values of the component properties.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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();
}

56
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 <qiang.xue@gmail.com>
* @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]].

11
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

5
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

Loading…
Cancel
Save