Commits

Bogdan Savluk  committed 0d2cff0

Fixed bug in behavior when tring to insert new record.

  • Participants
  • Parent commits eb5d26b

Comments (0)

Files changed (1)

File GalleryBehavior.php

     private $_gallery;
 
     /** Will create new gallery after save if no associated gallery exists */
-    public function afterSave($event)
+    public function beforeSave($event)
     {
-        parent::afterSave($event);
-        if (empty($this->getOwner()->{$this->idAttribute})) {
-            $gallery = new Gallery();
-            $gallery->name = $this->name;
-            $gallery->description = $this->description;
-            $gallery->versions = $this->versions;
-            $gallery->save();
+        parent::beforeSave($event);
+        if ($event->isValid) {
+            if (empty($this->getOwner()->{$this->idAttribute})) {
+                $gallery = new Gallery();
+                $gallery->name = $this->name;
+                $gallery->description = $this->description;
+                $gallery->versions = $this->versions;
+                $gallery->save();
 
-            $this->getOwner()->{$this->idAttribute} = $gallery->id;
-            $this->getOwner()->saveAttributes($this->getOwner()->getAttributes());
+                $this->getOwner()->{$this->idAttribute} = $gallery->id;
+            }
         }
     }