Last active
August 29, 2015 13:57
-
-
Save rapzo/9465881 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\models; | |
class BaseModel extends \lithium\data\Model { | |
public function save($entity, $data = null, array $options = []) { | |
$now = new DateTime(); | |
if (!$entity->exists()) { | |
$entity->created = $now; | |
} | |
$entity->updated = $now; | |
return parent::save($entity, $data, $options); | |
} | |
^ good | |
public static function __init() { | |
BaseMode::applyFilter('save', function ($self, $params, $chain) { | |
$now = new DateTime(); | |
if (!$params['entity']->exists()) { | |
$params['entity']->created = $now; | |
} | |
$params['entity']->updated = $now; | |
return $chain->next($self, $params, $chain); | |
}); | |
} | |
^ bad | |
/** | |
* If you really insist on this pattern i think this will do the same | |
*/ | |
public static function config(array $config = []) { | |
static::applyFilter('save', function ($self, $params, $chain) { | |
$now = new DateTime(); | |
if (!$params['entity']->exists()) { | |
$params['entity']->created = $now; | |
} | |
$params['entity']->updated = $now; | |
return $chain->next($self, $params, $chain); | |
}); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment