mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-05 23:47:16 +08:00
w
This commit is contained in:
@ -10,9 +10,25 @@
|
|||||||
namespace yii\base;
|
namespace yii\base;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Model is the base class providing the common features needed by data model objects.
|
* Model is the base class for data models.
|
||||||
*
|
*
|
||||||
* Model defines the basic framework for data models that need to be validated.
|
* Model implements the following commonly used features:
|
||||||
|
*
|
||||||
|
* - attribute declaration: by default, every public class member is considered as
|
||||||
|
* a model attribute
|
||||||
|
* - attribute labels: each attribute may be associated with a label for display purpose
|
||||||
|
* - massive attribute assignment
|
||||||
|
* - scenario-based validation
|
||||||
|
*
|
||||||
|
* 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]]
|
||||||
|
*
|
||||||
|
* You may directly use Model to store model data, or extend it with customization.
|
||||||
|
* You may also customize Model by attaching [[ModelBehavior|model behaviors]].
|
||||||
*
|
*
|
||||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
@ -139,6 +155,19 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
|
|||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Performs the data validation.
|
||||||
*
|
*
|
||||||
@ -216,13 +245,22 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
|
|||||||
public function afterValidate()
|
public function afterValidate()
|
||||||
{
|
{
|
||||||
if ($this->hasEventHandlers('onAfterValidate')) {
|
if ($this->hasEventHandlers('onAfterValidate')) {
|
||||||
$this->onAfterValidate(new CEvent($this));
|
$this->onAfterValidate(new Event($this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This event is raised by [[init]] when initializing the model.
|
||||||
|
* @param Event $event the event parameter
|
||||||
|
*/
|
||||||
|
public function onInit($event)
|
||||||
|
{
|
||||||
|
$this->raiseEvent(__METHOD__, $event);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This event is raised after the model instance is created by new operator.
|
* This event is raised after the model instance is created by new operator.
|
||||||
* @param CEvent $event the event parameter
|
* @param Event $event the event parameter
|
||||||
*/
|
*/
|
||||||
public function onAfterConstruct($event)
|
public function onAfterConstruct($event)
|
||||||
{
|
{
|
||||||
@ -240,7 +278,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This event is raised after the validation is performed.
|
* This event is raised after the validation is performed.
|
||||||
* @param CEvent $event the event parameter
|
* @param Event $event the event parameter
|
||||||
*/
|
*/
|
||||||
public function onAfterValidate($event)
|
public function onAfterValidate($event)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -22,9 +22,10 @@ class ModelBehavior extends Behavior
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Declares event handlers for owner's events.
|
* Declares event handlers for owner's events.
|
||||||
* The default implementation returns three event handlers:
|
* The default implementation returns the following event handlers:
|
||||||
*
|
*
|
||||||
* - `onAfterConstruct` event: [[afterConstruct]]
|
* - `onAfterConstruct` event: [[afterConstruct]]
|
||||||
|
* - `onInit` event: [[initModel]]
|
||||||
* - `onBeforeValidate` event: [[beforeValidate]]
|
* - `onBeforeValidate` event: [[beforeValidate]]
|
||||||
* - `onAfterValidate` event: [[afterValidate]]
|
* - `onAfterValidate` event: [[afterValidate]]
|
||||||
*
|
*
|
||||||
@ -35,11 +36,21 @@ class ModelBehavior extends Behavior
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'onAfterConstruct' => 'afterConstruct',
|
'onAfterConstruct' => 'afterConstruct',
|
||||||
|
'onInit' => 'initModel',
|
||||||
'onBeforeValidate' => 'beforeValidate',
|
'onBeforeValidate' => 'beforeValidate',
|
||||||
'onAfterValidate' => 'afterValidate',
|
'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.
|
* Responds to [[Model::onAfterConstruct]] event.
|
||||||
* Overrides this method if you want to handle the corresponding event of the [[owner]].
|
* Overrides this method if you want to handle the corresponding event of the [[owner]].
|
||||||
|
|||||||
Reference in New Issue
Block a user