Skip to content

Instantly share code, notes, and snippets.

@harikt
Created January 12, 2016 15:58
Show Gist options
  • Save harikt/143ab4960e18e0fb8f44 to your computer and use it in GitHub Desktop.
Save harikt/143ab4960e18e0fb8f44 to your computer and use it in GitHub Desktop.
<?php
namespace App\Model\Behavior;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
class HistoryBehavior extends Behavior
{
protected $_defaultConfig = [
'userIdCallback' => null,
'customActions' => [],
'userNameFields' => [
'firstname' => 'firstname',
'lastname' => 'lastname',
'id' => 'Users.id'
]
];
public function initialize(array $config)
{
parent::initialize($config);
}
public function beforeSave(Event $event, EntityInterface $entity, \ArrayObject $options)
{
echo "Before save";
exit;
}
public function afterSave(Event $event, EntityInterface $entity)
{
echo "After save";
exit;
}
}
<?php
namespace Lib\ORM;
class Table extends \Cake\ORM\Table
{
public function initialize(array $config)
{
$this->addBehavior('History');
parent::initialize($config);
}
}
$userTable = \Cake\ORM\TableRegistry::get('Users');
$user = new \App\Model\Entity\User();
$user->email = 'aa';
$user->username = 'aa';
$user->password = 'aa';
$userTable->save($user);
dump($userTable->behaviors()->loaded());
<?php
namespace App\Model\Table;
use Lib\ORM\Table;
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->table('users');
$this->addBehavior('History');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment