Beispiel #1
0
 public function save($runValidation = true, $attributes = null)
 {
     if ($this->photo) {
         // save related photo record
         $transaction = Yii::app()->db->beginTransaction();
         try {
             // save the event
             $ret = parent::save($runValidation, $attributes);
             if (!$ret) {
                 throw new CException(implode(';', $this->getAllErrorMessages()));
             }
             // add media record for file
             $media = new Media();
             $media->setAttributes(array('fileName' => $this->photo->getName(), 'mimetype' => $this->photo->type), false);
             $media->resolveNameConflicts();
             if (!$media->save()) {
                 throw new CException(implode(';', $media->getAllErrorMessages()));
             }
             // save the file
             $tempName = $this->photo->getTempName();
             $username = Yii::app()->user->getName();
             if (!FileUtil::ccopy($tempName, "uploads/protected/media/{$username}/{$media->fileName}")) {
                 throw new CException();
             }
             // relate file to event
             $join = new RelationshipsJoin('insert', 'x2_events_to_media');
             $join->eventsId = $this->id;
             $join->mediaId = $media->id;
             if (!$join->save()) {
                 throw new CException(implode(';', $join->getAllErrorMessages()));
             }
             $transaction->commit();
             return $ret;
         } catch (CException $e) {
             $transaction->rollback();
             return false;
         }
     } else {
         return parent::save($runValidation, $attributes);
     }
 }