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
cec59b1a
Commit
cec59b1a
authored
Feb 25, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Documented more ways of attaching behaviors
parent
94b414fa
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
3 deletions
+39
-3
behaviors.md
docs/guide/behaviors.md
+39
-3
No files found.
docs/guide/behaviors.md
View file @
cec59b1a
...
@@ -9,9 +9,13 @@ code execution. Unlike [PHP's traits](http://www.php.net/traits), behaviors can
...
@@ -9,9 +9,13 @@ code execution. Unlike [PHP's traits](http://www.php.net/traits), behaviors can
Using behaviors
Using behaviors
---------------
---------------
A behavior can be attached to any class that extends from
[
[yii\base\Component
]
]. In order to attach a behavior to a class,
A behavior can be attached to any class that extends from
[
[yii\base\Component
]
] either from code or via application
the component class must implement the
`behaviors`
config.
method. As an example, Yii provides the
[
[yii\behaviors\TimestampBehavior
]
] behavior for automatically updating timestamp
### Attaching behaviors via `behaviors` method
In order to attach a behavior to a class you can implement the
`behaviors`
method of the component.
As an example, Yii provides the
[
[yii\behaviors\TimestampBehavior
]
] behavior for automatically updating timestamp
fields when saving an
[
[yii\db\ActiveRecord|Active Record
]
] model:
fields when saving an
[
[yii\db\ActiveRecord|Active Record
]
] model:
```
php
```
php
...
@@ -69,6 +73,38 @@ class User extends ActiveRecord
...
@@ -69,6 +73,38 @@ class User extends ActiveRecord
}
}
```
```
### Attaching behaviors dynamically
Another way to attach a behavior to a component is calling
`attachBehavior`
method like the followig:
```
php
$component
=
new
MyComponent
();
$component
->
attachBehavior
();
```
### Attaching behaviors from config
One can attach a behavior to a component when configuring it with a configuration array. The syntax is like the
following:
```
php
return
[
// ...
'components'
=>
[
'myComponent'
=>
[
// ...
'as tree'
=>
[
'class'
=>
'Tree'
,
'root'
=>
0
,
],
],
],
];
```
In the config above
`as tree`
stands for attaching a behavior named
`tree`
, and the array will be passed to
[
[\Yii::createObject()
]
]
to create the behavior object.
Creating your own behaviors
Creating your own behaviors
---------------------------
---------------------------
...
...
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