예제 #1
0
 /**
  * Move the object to a new rank, and shifts the rank
  * Of the objects inbetween the old and new rank accordingly
  *
  * @param     integer   $newRank rank value
  * @param     PropelPDO $con optional connection
  *
  * @return    Product the current object
  *
  * @throws    PropelException
  */
 public function moveToRank($newRank, PropelPDO $con = null)
 {
     if ($this->isNew()) {
         throw new PropelException('New objects cannot be moved. Please use insertAtRank() instead');
     }
     if ($con === null) {
         $con = Propel::getConnection(ProductPeer::DATABASE_NAME);
     }
     if ($newRank < 1 || $newRank > ProductQuery::create()->getMaxRankArray($con)) {
         throw new PropelException('Invalid rank ' . $newRank);
     }
     $oldRank = $this->getSortableRank();
     if ($oldRank == $newRank) {
         return $this;
     }
     $con->beginTransaction();
     try {
         // shift the objects between the old and the new rank
         $delta = $oldRank < $newRank ? -1 : 1;
         ProductPeer::shiftRank($delta, min($oldRank, $newRank), max($oldRank, $newRank), $con);
         // move the object to its new rank
         $this->setSortableRank($newRank);
         $this->save($con);
         $con->commit();
         return $this;
     } catch (Exception $e) {
         $con->rollback();
         throw $e;
     }
 }