当前位置:网站首页>Yii2 behavior usage and its calling method
Yii2 behavior usage and its calling method
2022-07-25 23:08:00 【Drevindt】
yii2 Of Behavior In fact, that is yii Behavior in , You can expand existing classes , Without modifying existing classes , Expand the functions of classes . By binding behavior to a class , It can make the class have the properties and methods of the behavior itself , It's like these attributes and functions that a class already has .
Use :
First create a folder in the root directory , The name is components, Create another file named MyBehavior
namespace app\components;
use yii\db\ActiveRecord;
use yii\base\Behavior;
class MyBehavior extends Behavior
{
// Other code
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
];
}
public function beforeValidate($event)
{
// Processor method logic
$model = $this->owner;
$model->content .= '--beforeinsert';
}
}Call mode :
- Static call mode :
Static call mode , Is to call in the corresponding model . Then use the methods in the model normally ,
use app\components\HelloBehavior;
class Test extends ActiveRecord
{
public function behaviors()
{
// return parent::behaviors(); // TODO: Change the autogenerated stub
return [
'hello'=>HelloBehavior::className(),
];
}
}
- Dynamic call mode :
The dynamic call method is used in the controller ,new After a model , In the use of attachBehaviors
use app\components\HelloBehavior;
use yii\web\Controller;
use app\models\Test;
use app\components\HelloBehavior;
class TestController extends Controller
{
public function actionSave(){
$model = new Test();
$model->attachBehaviors([
HelloBehavior::className()
]);
$model->content = 'behavior test 1';
$model->create_at = date('Y-m-d H:i:s');
dd( $model->save() );
}
}
边栏推荐
- 理解的英文(言语理解)
- js正则表达式匹配ip地址(ip地址正则表达式验证)
- VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
- Oracle query statement summary
- 赋能合作伙伴,亚马逊云科技如何落地“扶上马,送一程”?
- [opencv] edge detection [API and source code implementation]
- Ma Tiji Wanmin hall Chef
- Circle detection and line detection of PCL
- Secure code warrior learning record (II)
- Experience of machine learning with Google Lab
猜你喜欢

Network security and level protection

The third experiment OSPF

VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.

Memory paging and tuning, kernel and user space

Mocha test

Learning notes of technical art hundred people plan (2) -- vector

Design of Butterworth filter and drawing of amplitude frequency characteristic curve

MathType installation and solution cannot solve the problem of crtl+v

General paging function

Basic knowledge of radar
随机推荐
Tips for using (1)
Notification设置的小图标显示的是小方块
Secure code warrior learning record (III)
Circle detection and line detection of PCL
asp日期函数(磁盘函数不正确怎么办)
如何获取广告服务流量变现数据,助力广告效果分析?
5 ROS仿真建模(3- rviz+gazebo+控制仿真机器人)
Hj7 take approximate value
Servlet overview
内存分页与调优,内核与用户空间
【接口性能优化】索引失效的原因以及如何进行SQL优化
Madness. Smbms (supermarket order management system)
The third experiment OSPF
Review of static routing
Design of Butterworth filter and drawing of amplitude frequency characteristic curve
Mongodb features, differences with MySQL, and application scenarios
Why is Google's internal tools not suitable for you?
[paper notes] a meta reinforcement learning algorithm for causal discovery
r语言绘图参数(R语言plot画图)
ffmpeg初次学习(仅针对编码)