/** * 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->aUser !== null) { if ($this->aUser->isModified() || $this->aUser->isNew()) { $affectedRows += $this->aUser->save($con); } $this->setUser($this->aUser); } if ($this->aRatingField !== null) { if ($this->aRatingField->isModified() || $this->aRatingField->isNew()) { $affectedRows += $this->aRatingField->save($con); } $this->setRatingField($this->aRatingField); } if ($this->aCourseInstructorAssociation !== null) { if ($this->aCourseInstructorAssociation->isModified() || $this->aCourseInstructorAssociation->isNew()) { $affectedRows += $this->aCourseInstructorAssociation->save($con); } $this->setCourseInstructorAssociation($this->aCourseInstructorAssociation); } if ($this->isNew()) { $this->modifiedColumns[] = CourseRatingPeer::ID; } // If this object has been modified, then save it to the database. if ($this->isModified()) { if ($this->isNew()) { $pk = CourseRatingPeer::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 += CourseRatingPeer::doUpdate($this, $con); } $this->resetModified(); // [HL] After being saved an object is no longer 'modified' } $this->alreadyInSave = false; } return $affectedRows; }
/** * Save the course_instructor_assocs * @param $instructor * @param $request * @return true if ready for saving, false otherwise */ protected function parseInsAssoc(Instructor $instructor, sfWebRequest $request) { $conn = Propel::getConnection(); // retrieve existing assoc objects $criteria = new Criteria(); $criteria->addAscendingOrderByColumn(CourseInstructorAssociationPeer::YEAR); $criteria->addAscendingOrderByColumn(CourseInstructorAssociationPeer::COURSE_ID); $extObjs = $instructor->getCourseInstructorAssociations($criteria, $conn); $delList = $extObjs; for ($i = $this->date["year"] + 1; $i >= $this->earliestYear; $i--) { for ($j = 1; $j <= 9; $j += 4) { $year = $i . $j; // first get an array of items $itemArr = array(); $token = strtok($request->getParameter("assoc[" . $year . "]"), $this->separator); while ($token !== false) { if (trim($token) != "") { $itemArr[] = $token; } $token = strtok($this->separator); } // check which ones exist, which ones are new and which ones need deletion foreach ($itemArr as $item) { $cCode = substr($item, 0, 8); $existed = false; foreach ($extObjs as $obj) { if ($obj->getCourseId() == $cCode && $obj->getYear() == $year) { $existed = true; $key = array_search($obj, $delList); if ($key !== false) { unset($delList[$key]); } break; } } if (!$existed) { // save the new assoc $assoc = new CourseInstructorAssociation(); $assoc->setCourseId($cCode); $assoc->setInstructorId($instructor->getId()); $assoc->setYear($year); $assoc->save($conn); } } } } // delete old assocs that no longer exist foreach ($delList as $obj) { $obj->delete($conn); } return true; }
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."); } }