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
36a58739
Commit
36a58739
authored
Jul 29, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
w
parent
19430d8d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
6 deletions
+55
-6
Model.php
framework/base/Model.php
+43
-5
ModelBehavior.php
framework/base/ModelBehavior.php
+12
-1
No files found.
framework/base/Model.php
View file @
36a58739
...
...
@@ -10,9 +10,25 @@
namespace
yii\base
;
/**
* Model is the base class
providing the common features needed by data model object
s.
* Model is the base class
for data model
s.
*
* 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>
* @since 2.0
...
...
@@ -140,6 +156,19 @@ 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]].
...
...
@@ -216,13 +245,22 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
public
function
afterValidate
()
{
if
(
$this
->
hasEventHandlers
(
'onAfterValidate'
))
{
$this
->
onAfterValidate
(
new
C
Event
(
$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.
* @param
C
Event $event the event parameter
* @param Event $event the event parameter
*/
public
function
onAfterConstruct
(
$event
)
{
...
...
@@ -240,7 +278,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
/**
* This event is raised after the validation is performed.
* @param
C
Event $event the event parameter
* @param Event $event the event parameter
*/
public
function
onAfterValidate
(
$event
)
{
...
...
framework/base/ModelBehavior.php
View file @
36a58739
...
...
@@ -22,9 +22,10 @@ class ModelBehavior extends Behavior
{
/**
* Declares event handlers for owner's events.
* The default implementation returns th
ree
event handlers:
* The default implementation returns th
e following
event handlers:
*
* - `onAfterConstruct` event: [[afterConstruct]]
* - `onInit` event: [[initModel]]
* - `onBeforeValidate` event: [[beforeValidate]]
* - `onAfterValidate` event: [[afterValidate]]
*
...
...
@@ -35,12 +36,22 @@ 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
...
...
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