Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
b158ff07
Commit
b158ff07
authored
Jul 31, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
w
parent
36a58739
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
83 additions
and
29 deletions
+83
-29
YiiBase.php
framework/YiiBase.php
+1
-1
Component.php
framework/base/Component.php
+1
-1
Initable.php
framework/base/Initable.php
+36
-0
Model.php
framework/base/Model.php
+41
-15
ModelBehavior.php
framework/base/ModelBehavior.php
+0
-11
todo.txt
todo.txt
+4
-1
No files found.
framework/YiiBase.php
View file @
b158ff07
...
@@ -380,7 +380,7 @@ class YiiBase
...
@@ -380,7 +380,7 @@ class YiiBase
$object
->
$key
=
$value
;
$object
->
$key
=
$value
;
}
}
if
(
$object
instanceof
\yii\base\
Component
)
{
if
(
$object
instanceof
\yii\base\
Initable
)
{
$object
->
init
();
$object
->
init
();
}
}
...
...
framework/base/Component.php
View file @
b158ff07
...
@@ -332,7 +332,7 @@ class Component
...
@@ -332,7 +332,7 @@ class Component
* )
* )
* ~~~
* ~~~
*
*
* Note that
the behavior classe
s must extend from [[Behavior]]. Behaviors declared
* Note that
a behavior clas
s must extend from [[Behavior]]. Behaviors declared
* in this method will be attached to the model when [[init]] is invoked.
* in this method will be attached to the model when [[init]] is invoked.
*
*
* @return array the behavior configurations.
* @return array the behavior configurations.
...
...
framework/base/Initable.php
0 → 100644
View file @
b158ff07
<?php
/**
* Initable interface file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 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
();
}
framework/base/Model.php
View file @
b158ff07
...
@@ -23,7 +23,6 @@ namespace yii\base;
...
@@ -23,7 +23,6 @@ namespace yii\base;
* Model also provides a set of events for further customization:
* Model also provides a set of events for further customization:
*
*
* - [[onAfterConstruct]]: an event raised at the end of constructor
* - [[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]]
* - [[onBeforeValidate]]: an event raised at the beginning of [[validate]]
* - [[onAfterValidate]]: an event raised at the end of [[validate]]
* - [[onAfterValidate]]: an event raised at the end of [[validate]]
*
*
...
@@ -33,7 +32,7 @@ namespace yii\base;
...
@@ -33,7 +32,7 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @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
static
$_attributes
=
array
();
// class name => array of attribute names
private
$_errors
;
// attribute name => array of errors
private
$_errors
;
// attribute name => array of errors
...
@@ -51,6 +50,46 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -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.
* Returns the list of attribute names.
* By default, this method returns all public non-static properties of the class.
* By default, this method returns all public non-static properties of the class.
* You may override this method to change the default.
* You may override this method to change the default.
...
@@ -156,19 +195,6 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
...
@@ -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.
* Performs the data validation.
*
*
* This method executes the validation rules as declared in [[rules]].
* This method executes the validation rules as declared in [[rules]].
...
...
framework/base/ModelBehavior.php
View file @
b158ff07
...
@@ -25,7 +25,6 @@ class ModelBehavior extends Behavior
...
@@ -25,7 +25,6 @@ class ModelBehavior extends Behavior
* The default implementation returns the following 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]]
*
*
...
@@ -36,22 +35,12 @@ class ModelBehavior extends Behavior
...
@@ -36,22 +35,12 @@ 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]].
* @param Event $event event parameter
* @param Event $event event parameter
...
...
todo.txt
View file @
b158ff07
- base
- base
* add more doc to Model
* error/exception handling
* error/exception handling
* security
* module
* application
* http exception
- validators
- validators
* CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
* CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
* FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD
* FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment