Skip to content

Commit

Permalink
Collection triggers events of each model to saved
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed May 28, 2015
1 parent cec1ad7 commit a9eb1cd
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,15 @@ public function validate ($attributes = null) {

public function beforeValidate () {
$event = new ModelEvent();
$this->trigger(self::EVENT_BEFORE_VALIDATE, $event);
$this->triggerAll(self::EVENT_BEFORE_VALIDATE, $event);

return $event->isValid;
}

public function afterValidate () {
$event = new ModelEvent();

$this->trigger(self::EVENT_AFTER_VALIDATE, $event);
$this->triggerAll(self::EVENT_AFTER_VALIDATE, $event);

return $event->isValid;
}
Expand All @@ -398,13 +398,13 @@ public function beforeSave ($insert = false) {
if ($this->isEmpty()) {
$event->isValid = false;
}
$this->trigger($insert ? self::EVENT_BEFORE_INSERT : self::EVENT_BEFORE_UPDATE, $event);
$this->triggerAll($insert ? self::EVENT_BEFORE_INSERT : self::EVENT_BEFORE_UPDATE, $event);

return $event->isValid;
}

public function afterSave () {
$this->trigger(self::EVENT_AFTER_SAVE);
$this->triggerAll(self::EVENT_AFTER_SAVE);
}


Expand All @@ -426,7 +426,7 @@ public function afterLoad () {
* @param ModelEvent $event
* @return bool whether is valid
*/
public function triggerAll ($name, ModelEvent $event = null) {
public function triggerModels ($name, ModelEvent $event = null) {
if ($event == null) {
$event = new ModelEvent();
}
Expand All @@ -438,6 +438,23 @@ public function triggerAll ($name, ModelEvent $event = null) {
return $event->isValid;
}

/**
* Calls [[triggerModels()]], then calls [[trigger()]]
*
* @param string $name the event name
* @param ModelEvent $event
* @return bool whether is valid
*/
public function triggerAll($name, ModelEvent $event = null) {
if ($event == null) {
$event = new ModelEvent();
}
if ($this->triggerModels($name, $event)) {
$this->trigger($name, $event);
}
return $event->isValid;
}

public function isConsistent () {
$new = $this->first->getIsNewRecord();
$className = $this->first->className();
Expand Down

0 comments on commit a9eb1cd

Please sign in to comment.