off() public static method

This method is the opposite of Event::on.
See also: on()
public static off ( string $class, string $name, callable $handler = null ) : boolean
$class string the fully qualified class name from which the event handler needs to be detached.
$name string the event name.
$handler callable the event handler to be removed. If it is `null`, all handlers attached to the named event will be removed.
return boolean whether a handler is found and detached.
Example #1
0
 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'));
 }
Example #2
0
 /**
  * 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);
     }
 }
 /**
  * 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']);
 }
 /**
  * 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']);
 }
 /**
  * @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);
         }
     }
 }
 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);
 }
Example #7
0
 /**
  * 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);
 }
Example #8
0
 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);
 }