/** * 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 ConnectionInterface $con optional connection * * @return $this|ChildMilestone the current object * * @throws PropelException */ public function moveToRank($newRank, ConnectionInterface $con = null) { if ($this->isNew()) { throw new PropelException('New objects cannot be moved. Please use insertAtRank() instead'); } if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(MilestoneTableMap::DATABASE_NAME); } if ($newRank < 1 || $newRank > ChildMilestoneQuery::create()->getMaxRankArray($con)) { throw new PropelException('Invalid rank ' . $newRank); } $oldRank = $this->getSortableRank(); if ($oldRank == $newRank) { return $this; } $con->transaction(function () use($con, $oldRank, $newRank) { // shift the objects between the old and the new rank $delta = $oldRank < $newRank ? -1 : 1; ChildMilestoneQuery::sortableShiftRank($delta, min($oldRank, $newRank), max($oldRank, $newRank), $con); // move the object to its new rank $this->setSortableRank($newRank); $this->save($con); }); return $this; }