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.");
     }
 }
Ejemplo n.º 2
0
 /**
  * Declares an association between this object and a CourseInstructorAssociation object.
  *
  * @param      CourseInstructorAssociation $v
  * @return     CourseRating The current object (for fluent API support)
  * @throws     PropelException
  */
 public function setCourseInstructorAssociation(CourseInstructorAssociation $v = null)
 {
     if ($v === null) {
         $this->setCourseInsId(NULL);
     } else {
         $this->setCourseInsId($v->getId());
     }
     $this->aCourseInstructorAssociation = $v;
     // Add binding for other direction of this n:n relationship.
     // If this object has already been added to the CourseInstructorAssociation object, it will not be re-added.
     if ($v !== null) {
         $v->addCourseRating($this);
     }
     return $this;
 }
 /**
  * Adds an object to the instance pool.
  *
  * Propel keeps cached copies of objects in an instance pool when they are retrieved
  * from the database.  In some cases -- especially when you override doSelect*()
  * methods in your stub classes -- you may need to explicitly add objects
  * to the cache in order to ensure that the same objects are always returned by doSelect*()
  * and retrieveByPK*() calls.
  *
  * @param      CourseInstructorAssociation $value A CourseInstructorAssociation object.
  * @param      string $key (optional) key to use for instance map (for performance boost if key was already calculated externally).
  */
 public static function addInstanceToPool(CourseInstructorAssociation $obj, $key = null)
 {
     if (Propel::isInstancePoolingEnabled()) {
         if ($key === null) {
             $key = (string) $obj->getId();
         }
         // if key === null
         self::$instances[$key] = $obj;
     }
 }