Created
January 12, 2016 15:58
-
-
Save harikt/143ab4960e18e0fb8f44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Lib\ORM; | |
class Table extends \Cake\ORM\Table | |
{ | |
public function initialize(array $config) | |
{ | |
$this->addBehavior('History'); | |
parent::initialize($config); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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