/**
  * Performs the work of inserting or updating the row in the database.
  *
  * If the object is new, it inserts it; otherwise an update is performed.
  * All related objects are also updated in this method.
  *
  * @param      PropelPDO $con
  * @return     int The number of rows affected by this insert/update and any referring fk objects' save() operations.
  * @throws     PropelException
  * @see        save()
  */
 protected function doSave(PropelPDO $con)
 {
     $affectedRows = 0;
     // initialize var to track total num of affected rows
     if (!$this->alreadyInSave) {
         $this->alreadyInSave = true;
         // We call the save method on the following object(s) if they
         // were passed to this object by their coresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         if ($this->aInstructor !== null) {
             if ($this->aInstructor->isModified() || $this->aInstructor->isNew()) {
                 $affectedRows += $this->aInstructor->save($con);
             }
             $this->setInstructor($this->aInstructor);
         }
         if ($this->aCourse !== null) {
             if ($this->aCourse->isModified() || $this->aCourse->isNew()) {
                 $affectedRows += $this->aCourse->save($con);
             }
             $this->setCourse($this->aCourse);
         }
         if ($this->isNew()) {
             $this->modifiedColumns[] = CourseInstructorAssociationPeer::ID;
         }
         // If this object has been modified, then save it to the database.
         if ($this->isModified()) {
             if ($this->isNew()) {
                 $pk = CourseInstructorAssociationPeer::doInsert($this, $con);
                 $affectedRows += 1;
                 // we are assuming that there is only 1 row per doInsert() which
                 // should always be true here (even though technically
                 // BasePeer::doInsert() can insert multiple rows).
                 $this->setId($pk);
                 //[IMV] update autoincrement primary key
                 $this->setNew(false);
             } else {
                 $affectedRows += CourseInstructorAssociationPeer::doUpdate($this, $con);
             }
             $this->resetModified();
             // [HL] After being saved an object is no longer 'modified'
         }
         if ($this->collCourseRatings !== null) {
             foreach ($this->collCourseRatings as $referrerFK) {
                 if (!$referrerFK->isDeleted()) {
                     $affectedRows += $referrerFK->save($con);
                 }
             }
         }
         if ($this->collAutoCourseRatings !== null) {
             foreach ($this->collAutoCourseRatings as $referrerFK) {
                 if (!$referrerFK->isDeleted()) {
                     $affectedRows += $referrerFK->save($con);
                 }
             }
         }
         $this->alreadyInSave = false;
     }
     return $affectedRows;
 }
Ejemplo n.º 2
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Instructor();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Instructor'])) {
         $model->attributes = $_POST['Instructor'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 public function saveToDatabase()
 {
     if (isset($this->_infoArr) && isset($this->_mappingArr)) {
         $err = "";
         $conn = Propel::getConnection();
         $dt = date("Y-m-d, H:i:s");
         $len = count($this->_infoArr);
         for ($i = 0; $i < $len; $i++) {
             $course = CoursePeer::retrieveByPK($this->_infoArr[$i]["courseCode"]);
             if (!isset($course)) {
                 // new course found, save to db
                 $course = new Course();
                 $course->setDescr($this->_infoArr[$i]["courseName"]);
                 $course->setIsEng(1);
                 $course->setDeptId(substr($this->_infoArr[$i]["courseCode"], 0, 3));
                 $course->setId($this->_infoArr[$i]["courseCode"]);
                 $course->save($conn);
             } elseif ($course->getDescr() == $course->getId()) {
                 // exam importer registers course description as course id
                 // if we encounter this situation, amend it with the proper description
                 $course->setDescr($this->_infoArr[$i]["courseName"]);
                 $course->save($conn);
             }
             try {
                 $instr = InstructorPeer::findInstructorByName($this->_infoArr[$i]["instrFirstName"], $this->_infoArr[$i]["instrLastName"], $conn);
             } catch (Exception $e) {
                 if ($e->getCode() == 1) {
                     // no instructor found
                     $instr = new Instructor();
                     $instr->setFirstName($this->_infoArr[$i]["instrFirstName"]);
                     $instr->setLastName($this->_infoArr[$i]["instrLastName"]);
                     $instr->setDeptId($course->getDeptId());
                     $instr->save($conn);
                 } else {
                     // TODO: big problem, duplicate instructors found
                     // log error and move on
                     continue;
                 }
             }
             // create CourseInstructorAssociation if it doesn't exist
             try {
                 $assoc = CourseInstructorAssociationPeer::findForYearAndInstructorIdAndCourseId($this->_year, $course->getId(), $instr->getId(), $conn);
             } catch (Exception $e) {
                 if ($e->getCode() == 1) {
                     // create new object
                     $assoc = new CourseInstructorAssociation();
                     $assoc->setYear($this->_year);
                     $assoc->setCourseId($course->getId());
                     $assoc->setInstructorId($instr->getId());
                     $assoc->save($conn);
                 } else {
                     // TODO: big problem, duplicate assocs found
                     // log error and move on
                     continue;
                 }
             }
             // we can now save the real rating data
             $ratingArr = $this->_ratingArr[$i];
             foreach ($ratingArr as $fieldId => $data) {
                 foreach ($data as $rating => $number) {
                     $ratingObj = new AutoCourseRating();
                     $ratingObj->setFieldId($fieldId);
                     $ratingObj->setRating($rating);
                     $ratingObj->setNumber($number);
                     $ratingObj->setImportDt($dt);
                     $ratingObj->setCourseInsId($assoc->getId());
                     $ratingObj->save();
                 }
             }
         }
     } else {
         throw new Exception("readCsv method has not been called.");
     }
 }