本文整理汇总了PHP中yii\base\Event::off方法的典型用法代码示例。如果您正苦于以下问题:PHP Event::off方法的具体用法?PHP Event::off怎么用?PHP Event::off使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\base\Event
的用法示例。
在下文中一共展示了Event::off方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testOff
public function testOff()
{
$handler = function ($event) {
$this->counter++;
};
$this->assertFalse(Event::hasHandlers(Post::className(), 'save'));
Event::on(Post::className(), 'save', $handler);
$this->assertTrue(Event::hasHandlers(Post::className(), 'save'));
Event::off(Post::className(), 'save', $handler);
$this->assertFalse(Event::hasHandlers(Post::className(), 'save'));
}
示例2: unRegisterAction
/**
* Removes an event
*
* Warning: If $event is NULL this method will delete all attached events with that name.
*
* @param mixed $name
* @param null $handler
*/
public static function unRegisterAction($name, $handler = null)
{
// if the $name is an array we are using special feature that
// lets system to add new events to third part packages
// but this is very limited, only will work with the common events
// and if there are specially added events into the package
if (is_array($name)) {
$class = $name[0];
$name = $name[1];
Event::off($class, $name, $handler);
} else {
self::getResponseEvent()->off($name, $handler);
}
}
示例3: addValidators
/**
* Add needed validators on after find model event
* @param Event $event
*/
public function addValidators(Event $event)
{
/** @var ActiveRecord|SyncableBehavior $model */
$model = $event->sender;
/** @var |\yii\db\ActiveRecord $modelClass */
$requestUpdatedAt = \Yii::$app->request->getBodyParam($model->timestampColumn);
// unset timestampColumn value when it does not send by client for trigger required validator
if (empty($requestUpdatedAt)) {
$model->{$model->timestampColumn} = null;
}
foreach ($this->createValidators($model) as $validator) {
$model->validators->append($validator);
}
// on this stage it is not needed anymore to handle this event
Event::off(ActiveRecord::className(), ActiveRecord::EVENT_INIT, [$this, 'onActiveRecordInit']);
}
示例4: applyLatestOnlyCondition
/**
* Applies latest only condition for syncable behaved models
*
* @param Event $event
*/
public function applyLatestOnlyCondition(Event $event)
{
/** @var \yii\db\BaseActiveRecord|\zarv1k\sync\twoway\behavior\model\SyncableBehavior $model */
$model = $event->data;
/** @var \yii\db\ActiveRecordInterface $modelClass */
$modelClass = $this->owner->modelClass;
/** @var ActiveQuery $activeQuery */
$activeQuery = $event->sender;
$updatedAfterDate = \Yii::$app->request->getQueryParam($model->timestampQueryParam);
$orderBy = [$model->timestampColumn => SORT_ASC];
foreach ($modelClass::primaryKey() as $column) {
$orderBy[$column] = SORT_ASC;
}
$activeQuery->andFilterWhere(['>', $model->timestampColumn, $updatedAfterDate])->addOrderBy($orderBy);
Event::off(ActiveQuery::className(), ActiveQuery::EVENT_INIT, [$this, 'applyLatestOnlyCondition']);
}
示例5: unbind
/**
* @param string | Component $emitter Class or class name that emits events
* @param string | object $listener Listener class or class name that will listen to those events
*/
public function unbind($emitter, $listener)
{
$emitterReflection = new \ReflectionClass($emitter);
$listenerReflection = new \ReflectionClass($listener);
foreach ($this->eventsProvider->getEventNames($emitterReflection) as $eventName) {
$methodReflection = $this->methodFinder->getMethodForEvent($listenerReflection, $eventName);
if ($methodReflection === false) {
continue;
}
$handler = $this->createHandler($listener, $methodReflection);
if ($handler === false) {
continue;
}
if (is_string($emitter)) {
Event::off($emitter, $eventName, $handler);
} elseif ($emitter instanceof Component) {
$emitter->off($eventName, $handler);
}
}
}
示例6: testAfterFind
public function testAfterFind()
{
/** @var \yii\db\ActiveRecordInterface $customerClass */
$customerClass = $this->getCustomerClass();
/** @var BaseActiveRecord $orderClass */
$orderClass = $this->getOrderClass();
/** @var TestCase|ActiveRecordTestTrait $this */
$afterFindCalls = [];
Event::on(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND, function ($event) use(&$afterFindCalls) {
/** @var BaseActiveRecord $ar */
$ar = $event->sender;
$afterFindCalls[] = [get_class($ar), $ar->getIsNewRecord(), $ar->getPrimaryKey(), $ar->isRelationPopulated('orders')];
});
$customer = $customerClass::findOne(1);
$this->assertNotNull($customer);
$this->assertEquals([[$customerClass, false, 1, false]], $afterFindCalls);
$afterFindCalls = [];
$customer = $customerClass::find()->where(['id' => 1])->one();
$this->assertNotNull($customer);
$this->assertEquals([[$customerClass, false, 1, false]], $afterFindCalls);
$afterFindCalls = [];
$customer = $customerClass::find()->where(['id' => 1])->all();
$this->assertNotNull($customer);
$this->assertEquals([[$customerClass, false, 1, false]], $afterFindCalls);
$afterFindCalls = [];
$customer = $customerClass::find()->where(['id' => 1])->with('orders')->all();
$this->assertNotNull($customer);
$this->assertEquals([[$this->getOrderClass(), false, 1, false], [$customerClass, false, 1, true]], $afterFindCalls);
$afterFindCalls = [];
if ($this instanceof \yiiunit\extensions\redis\ActiveRecordTest) {
// TODO redis does not support orderBy() yet
$customer = $customerClass::find()->where(['id' => [1, 2]])->with('orders')->all();
} else {
// orderBy is needed to avoid random test failure
$customer = $customerClass::find()->where(['id' => [1, 2]])->with('orders')->orderBy('name')->all();
}
$this->assertNotNull($customer);
$this->assertEquals([[$orderClass, false, 1, false], [$orderClass, false, 2, false], [$orderClass, false, 3, false], [$customerClass, false, 1, true], [$customerClass, false, 2, true]], $afterFindCalls);
$afterFindCalls = [];
Event::off(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND);
}
示例7: testMissingTranslationEvent
/**
* https://github.com/yiisoft/yii2/issues/2519
*/
public function testMissingTranslationEvent()
{
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
$this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE'));
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
Event::on(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION, function ($event) {
});
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
$this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE'));
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
Event::off(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION);
Event::on(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION, function ($event) {
if ($event->message == 'New missing translation message.') {
$event->translatedMessage = 'TRANSLATION MISSING HERE!';
}
});
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
$this->assertEquals('Another missing translation message.', $this->i18n->translate('test', 'Another missing translation message.', [], 'de-DE'));
$this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE'));
$this->assertEquals('TRANSLATION MISSING HERE!', $this->i18n->translate('test', 'New missing translation message.', [], 'de-DE'));
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
Event::off(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION);
}
示例8: testAfterFindGet
public function testAfterFindGet()
{
/* @var $customerClass BaseActiveRecord */
$customerClass = $this->getCustomerClass();
$afterFindCalls = [];
Event::on(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND, function ($event) use(&$afterFindCalls) {
/* @var $ar BaseActiveRecord */
$ar = $event->sender;
$afterFindCalls[] = [get_class($ar), $ar->getIsNewRecord(), $ar->getPrimaryKey(), $ar->isRelationPopulated('orders')];
});
$customer = Customer::get(1);
$this->assertNotNull($customer);
$this->assertEquals([[$customerClass, false, 1, false]], $afterFindCalls);
$afterFindCalls = [];
$customer = Customer::mget([1, 2]);
$this->assertNotNull($customer);
$this->assertEquals([[$customerClass, false, 1, false], [$customerClass, false, 2, false]], $afterFindCalls);
$afterFindCalls = [];
Event::off(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND);
}