Many of you are not sure about the events and behaviors of YIi2, but you will find that it is not as complicated as you think.
Before studying this article, it is recommended to review the following content, which will be helpful for this study.
- Step by step – learn the five built-in behavior classes yii2 gives us
- Yii2 event study
As usual, in this article we’ll learn how to use it, and in the next we’ll look at how it works.
To prepare
Let’s use the examples from the previous articles
namespace app\components;
use yii\base\Behavior;
class HelloBehavior extends Behavior {}Copy the code
Again, HelloBehavior, one thing I want to do now is bind this behavior to the User model (an AR, the User table that manages the database), and whenever a new member is created, add a “+” sign to its USERNAME.
First of all, there are many ways to do this, and we do it behaviorally.
Every time I create a new member, I naturally think that AR has a built-in event called ActiveRecord::EVENT_BEFORE_INSERT, so use that.
You didn’t know about the incident? Go check out the shorthand portal
I know that the Behavior class has a function called Events that returns all related events, so let’s do that
namespace app\components;
use yii\base\Behavior;
class HelloBehavior extends Behavior {
public function events(a){
return [
ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',]; }public function beforeInsert(a){
$owner = $this->owner;
if ($owner->getIsNewRecord()) {
$owner->username .= '+'; }}}Copy the code
The transformation is simple. Use the Events function to bind the behavior to an event that is triggered by the component to which the behavior belongs. Then define an event handler in the behavior, and add a “+” after it when creating a new user.
As you can see so far, we tried to change the contents of User’s username, but we didn’t make any changes to the User class.
To verify
There are two ways to verify, one is to statically bind the User class, one is to dynamically bind. (Click on the portal if you don’t know the static binding)
Static binding
Static binding We need to configure the Behaviors () of the User class.
public function behaviors(a){
return [
'hello'= > ['class'=>HelloBehavior::className()
]
];
}Copy the code
Injecting HelloBehavior into the User class, we modify the User behaviors(). The behaviors() change a little, but not much. The behaviors() is more like a configuration, and I don’t change the User’s business logic.
Next, write the following code in one of the controller’s actions
$model = new User();
$model->username = 'abei2017';
$model->save();Copy the code
Look at the results
There we go. We affect the functionality of the User class through a combination of actions and events without changing the business logic of the User class.
Dynamic binding
To demonstrate dynamic binding, the behaviors() functions of users are not changed. Dynamic binding takes place within actions
$model = new User();
$model->attachBehaviors([
HelloBehavior::className()
]);
$model->username = 'abei1982';
$model->save();Copy the code
The dynamic method is the binding of the behavior of the User object after it is generated, and we have not made any changes to the User class at this point.
Look at the results.
And that’s it.
Some doubt
I know you have a lot of questions right now, like
- What are events for?
- How to handle custom events?
- How exactly does this thing work?
- Etc., etc.