protected function moveTo(Model_Ordered $target, $aftertarget = true) { if ($aftertarget) { $above = $target; $below = $target->getNext(); if (!$below) { $below = clone $target; $below->order++; } else { if ($below->getId() == $this->getId()) { return; } } } else { $above = $target->getPrev(); $below = $target; if (!$above) { $above = clone $target; $above->order--; } else { if ($above->getId() == $this->getId()) { return; } } } $delta = $below->order - $above->order; $this->order = $above->order + 1; $this->_db->update($this->_table, array($this->_order_by_field => $this->order), array($this->_pk => $this->getId())); if ($delta < 2) { $update = $above->getNextId(0); $update = array_diff($update, array($this->getId())); if ($update) { $delta = 2 - $delta; $this->_db->update($this->_table, "{$this->_order_by_field} = {$this->_order_by_field} + {$delta}", array($this->_pk => $update)); } } }