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
55e954b6
Commit
55e954b6
authored
Nov 29, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Docs on using behaviors
parent
05272b71
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
2 deletions
+39
-2
behaviors.md
docs/guide/behaviors.md
+39
-2
No files found.
docs/guide/behaviors.md
View file @
55e954b6
Behaviors
Behaviors
=========
=========
TDB
A behavior (also knows as mixin) can be used to enhance the functionality of an existing component without modifying its
\ No newline at end of file
code. In particular, it can "inject" its own methods and properties into the component and make them directly accessible
via the component. It can also respond to the events triggered in the component and thus intercept the normal
code execution. Unlike PHP traits, behaviors could be attached to classes at runtime.
Using behaviors
---------------
Behavior can be attached to any class that extends from
`Component`
. In order to do so you need to implement
`behaviors`
method. Yii provides
`AutoTimestamp`
behavior that handles updating timestamp fields on saving active record model.
```
php
class
User
extends
ActiveRecord
{
// ...
public
function
behaviors
()
{
return
[
'timestamp'
=>
[
'class'
=>
'yii\behaviors\AutoTimestamp'
,
'attributes'
=>
[
ActiveRecord
::
EVENT_BEFORE_INSERT
=>
[
'create_time'
,
'update_time'
],
ActiveRecord
::
EVENT_BEFORE_UPDATE
=>
'update_time'
,
],
],
];
}
}
```
In the above
`class`
value is a string containing fully qualified behavior class name. All the other key-values are
assigned to corresponding properties of the class.
Creating your own behaviors
---------------------------
TBD
\ No newline at end of file
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