Skip to content

Instantly share code, notes, and snippets.

@nojimage
Created June 9, 2016 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nojimage/5974027d544afb48323b7d9106e0a340 to your computer and use it in GitHub Desktop.
Save nojimage/5974027d544afb48323b7d9106e0a340 to your computer and use it in GitHub Desktop.
ビヘイビアが実行するイベントをOn/Offするやつ
<?php
namespace App\Model\Behavior;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventListenerInterface;
/**
* ビヘイビアが実行するイベントをコントロール
*/
trait BehaviorEventTrait
{
/**
* ビヘイビアイベントの停止
*
* @param string $name
* @param array $options
* @return void
*/
public function stopBehaviorEvent($name, array $options = [])
{
if (!$this->_behaviors->has($name)) {
return;
}
$behavior = $this->_behaviors->get($name);
if ($this->_behaviors instanceof EventDispatcherInterface && $behavior instanceof EventListenerInterface) {
$this->_behaviors->eventManager()->off($behavior);
}
}
/**
* ビヘイビアイベントの再開
*
* @param string $name
* @param array $options
* @return void
*/
public function restoreBehaviorEvent($name, array $options = [])
{
if (!$this->_behaviors->has($name)) {
return;
}
$behavior = $this->_behaviors->get($name);
if ($this->_behaviors instanceof EventDispatcherInterface && $behavior instanceof EventListenerInterface) {
$this->_behaviors->eventManager()->on($behavior);
}
}
}
@nojimage
Copy link
Author

nojimage commented Jun 9, 2016

Tableクラスでuse。
プラグイン化は後ほど。 #たぶん

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment