Reputation: 159
I need to trigger beforeSave() and afterSave() callbacks on updateAll(). I use updateAll() because I need update ID field.. (I knew it kinda 'wrong' about editable-ID thing, but this database I used is already messy like that).
What I need to do is, trigger the updateAll() to fire beforeSave() and afterSave(), just like save() does.
Here is a hack of updateAll() in CakePHP 1.3
function updateAll($fields, $conditions = true) {
$args = func_get_args();
$output = call_user_func_array(array('parent', 'updateAll'), $args);
if ($output) {
$created = false;
$options = array();
$this->Behaviors->trigger($this, 'afterSave', array(
$created,
$options,
));
$this->afterSave($created);
$this->_clearCache();
return true;
}
return false;
}
Then this is the code I adjusted in CakePHP 2.3
function updateAll($fields, $conditions = true) {
$args = func_get_args();
$output = call_user_func_array(array('parent', 'updateAll'), $args);
if ($output) {
$created = false;
$options = array();
$event = new CakeEvent('Model.afterSave', $this, array($created, $options));
$this->getEventManager()->dispatch($event);
$this->afterSave($created);
$this->_clearCache();
return true;
}
return false;
}
The problem is, what if I wanna call beforeSave() in the updateAll?
Upvotes: 1
Views: 1564
Reputation: 159
What a dumb-question I had.
Here is the code:
function updateAll($fields, $conditions = true) {
$options = array();
$event = new CakeEvent('Model.beforeSave', $this, array($options) );
list($event->break, $event->breakOn) = array(true, array(false, null));
$this->getEventManager()->dispatch($event);
$args = func_get_args();
$output = call_user_func_array(array('parent', 'updateAll'), $args);
if ($output) {
$created = false;
$options = array();
$event = new CakeEvent('Model.afterSave', $this, array($created, $options));
$this->getEventManager()->dispatch($event);
$this->afterSave($created);
$this->_clearCache();
return true;
}
return false;
}
Upvotes: 1