2016 年 10 月

浏览模式: 标准 | 列表 | 图文
Yii2 静态方法 中怎么触发事件

Yii2 静态方法 中怎么触发事件

在ActiveRecord的init中写了一个简单的事件触发:

public function init(){
$event = new XxxEvent();
$this->on(self::EVENT_AFTER_UPDATE,[$event,'doSomeThing']);
parent::init();
}

就是这么简单,然后在程序中有一段触发:

public static xxx(){
self::updateAllCounters([],[]);
}

然后发现,死活没有触发。追了一下代码发现,updateAllCounters没有从AR走,而是直接从db->createCommand()->execute走了

于是在下面加了一行:

self::trigger(self::EVENT_AFTER_UPDATE);

然而直接报错,说是trigger不支持静态调用,哦。。。trigger是非静态方法。

于是最后只能用万能的call_user_func_array的方法了:

call_user_func_array([new self,'trigger'],[self::EVENT_AFTER_UPDATE]);

有点贱,但总算实现了。

Yii2 updateAll tips

Yii2 updateAll tips

Yii2 中的updateAll可以接受三个参数,$attributes,$condition,$params = null

第一个参数是要更新的值,第二个是条件,如果第二个条件中用了占位符,就必须 要有第三个条件。
平时我们查询 的时候用andWhere()可以连接很多参数,那么updateAll的时候,如果有多个参数怎么办?
Test::updateAll(['status'=>1],['status'=>0,'flag'=>1]);
如果参数都是固定值,这样的写法当然OK,如果有范围值就只能这样写了,比如flag != 1怎么办?
Test::updateAll(['status'=>1],['and',['status'=>0],['<>','flag',1]]);
Over
PS:参考:http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail
PHP 7 DomDocument not found

PHP 7 DomDocument not found

升到PHP7后,当用到DomDocument的时候,报错说class DomDocument not found。。。

用php -m|grep xml,可以看到libxml是被加载的。
根据官方文档:
This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default.
证明没有错。
事实上也有很多人遇到了这个问题,怎么破?对于我用apt-get install的人来说,实在太方便了。
直接:
apt-get install php7.0-xml
搞定。
参考:http://stackoverflow.com/questions/35668702/php-7-domdocument-not-found