Skip to content

Instantly share code, notes, and snippets.

@rapzo
Last active August 29, 2015 13:57
Show Gist options
  • Save rapzo/9465881 to your computer and use it in GitHub Desktop.
Save rapzo/9465881 to your computer and use it in GitHub Desktop.
<?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