Move model relative other
@param ActiveRecord $model model to move
@param string $position moving position: after or before
551692
public function relativeMove($model, $position)
    {
        $conditionAttributes = (array)$this->conditionAttributes;
        $owner = $this->owner;

        if (!empty($conditionAttributes)) {
            $sameCondition = true;
            foreach ($conditionAttributes as $attr) {
                if ($owner->getAttribute($attr) != $model->getAttribute($attr)) {
                    $sameCondition = false;
                    break;
                }
            }
            if (!$sameCondition) {
                // move in other condition category
                $this->moveToTop();
                // update condition attribute
                $condition = [];
                foreach ($conditionAttributes as $attr) {
                    $condition[$attr] = $model->getAttribute($attr);
                }
                $condition[$this->sortAttribute] = $owner->find()->andWhere($this->getCondition())->count() - 1;
                $owner->updateAttributes($condition);
            }
        }
        // calculate pos change
        $currentPos = $owner->getAttribute($this->sortAttribute);
        $destinationPos = $model->getAttribute($this->sortAttribute);
        if ($position == 'after') {
            $newPos = $destinationPos > $currentPos ? $destinationPos - 1 : $destinationPos;
        } else {
            $newPos = $destinationPos > $currentPos ? $destinationPos : $destinationPos + 1;
        }
        $this->moveToPosition($newPos);
    }
1041235
public function movePageDisplayOrderToSibling(Page $c, $position = 'before')
    {
        $myCID = $this->getCollectionPointerOriginalID() ?: $this->getCollectionID();
        $relatedCID = $c->getCollectionPointerOriginalID() ?: $c->getCollectionID();
        $pageIDs = [];
        $db = Database::connection();
        $r = $db->executeQuery('select cID from Pages where cParentID = ? and cID <> ? order by cDisplayOrder asc', [$this->getCollectionParentID(), $myCID]);
        while (($cID = $r->fetchColumn()) !== false) {
            if ($cID == $relatedCID && $position == 'before') {
                $pageIDs[] = $myCID;
            }
            $pageIDs[] = $cID;
            if ($cID == $relatedCID && $position == 'after') {
                $pageIDs[] = $myCID;
            }
        }
        $displayOrder = 0;
        foreach ($pageIDs as $cID) {
            $co = self::getByID($cID);
            $co->updateDisplayOrder($displayOrder);
            ++$displayOrder;
        }
    }
825903
def follow_model(self, model):
        
        if model:
          self.models_by_name[model.__name__.lower()] = model
          signals.post_save.connect(create_or_update, sender=model)
          signals.post_delete.connect(self.remove_orphans, sender=model)
1269263
public function setPosition(Model $model, $position)
    {
        if ($position === $model->getListifyPosition()) {
            return $position;
        }

        switch ($position) {

            case null:
            case OrderablePosition::REMOVE:
                $model->removeFromList();
                break;

            case OrderablePosition::UP:
                $model->moveHigher();
                break;

            case OrderablePosition::DOWN:
                $model->moveLower();
                break;

            case OrderablePosition::TOP:
                if ($model->isInList()) {
                    $model->moveToTop();
                } else {
                    $model->insertAt();
                }
                break;

            case OrderablePosition::BOTTOM:
                if ($model->isInList()) {
                    $model->moveToBottom();
                } else {
                    $model->insertAt();
                    $model->moveToBottom();
                }
                break;

            default:
                $position = (int) $position;

                $model->insertAt($position);
        }

        return $model->getListifyPosition();
    }
1143355
public function moveBefore($entity)
    {
        if ($this->parent_id == $entity->parent_id) {
            $this->position = $entity->position - 1;
            $this->save();
        } else {
            $this->moveTo($entity->position - 1, $entity->parent_id);
        }
    }
1074290
protected function doRemovePositions(Sport $model, $data) {
		$errors = [];
		foreach ($data as $entry) {
			if (!isset($entry['id'])) {
				$errors[] = 'Missing id for Position';
			} else {
				$related = PositionQuery::create()->findOneById($entry['id']);
				$model->removePosition($related);
			}
		}

		if (count($errors) > 0) {
			return new ErrorsException($errors);
		}
	}
629673
function( cb ){
              if( ! self.positionField ){
                cb( null );
              } else {

                // If repositioning is required, do it
                if( self.positionField ){
                  var where, beforeId;
                  if( ! options.position ){
                    where = 'end';
                  } else {
                    where = options.position.where;
                    beforeId = options.position.beforeId;
                  }
                  self.reposition( recordWithLookups, where, beforeId, cb );
                }
              }
            }
368101
def move_to(self, target, position='first-child'):
        """"""

        # Invalidate both in case position matters,
        # otherwise only target is needed.
        self.invalidate()
        target.invalidate()
        super(Page, self).move_to(target, position=position)
489296
public function performStoreAfter(Model $model, $source, $value)
    {
        $value = $this->adjustValue($value);

        $model->retag($value);
    }
856854
public function afterInsert($model, $id)
    {
        if ($this->weak) {
            return;
        }

        $this->save_buffer[$this->foreign_field] =
            isset($this->join) ? $this->join->id : $id;

        $persistence = $this->persistence ?:
            $this->owner->persistence;

        $this->id = $persistence->insert(
            $model,
            $this->save_buffer,
            $this->foreign_table
        );
    }
1062988
public function attach($model, string $relation, Model $value)
    {
        if (! $model instanceof Model) {
            $model = $this->find($model);
        }

        $model->$relation()->attach($value);
    }
750989
public function remove($model)
    {
        $class    = $this->manager->getClassMetadata(get_class($model));
        $managers = $class->getFieldManagerNames();
        $priority = $this->manager->getPool()->getPriority('transaction');

        foreach ($managers as $key => $managerName) {
            if (isset($priority[$managerName])) {
                $this->manager->remove($model);

                unset($managers[$key]); // dereference index
            }
        }

        foreach ($managers as $managerName) {
            $this->addQueue(self::QUEUE_ACTION_REMOVE, $managerName, $model);
        }
    }
1199390
private void moveTo(int position) {
    while (position >= currentPosition) {
      try {
        currentPosition = ignoreSpans.nextStartPosition();
        if (currentPosition != Spans.NO_MORE_POSITIONS
            && currentPosition >= minimumPosition) {
          if (!baseEndPositionList.containsKey(currentPosition)) {
            baseEndPositionList.put(currentPosition, new HashSet<Integer>());
            maxBaseEndPosition.put(currentPosition, currentPosition);
          } else {
            maxBaseEndPosition.put(currentPosition,
                Math.max(maxBaseEndPosition.get(currentPosition),
                    ignoreSpans.endPosition()));
          }
          if (!baseStartPositionList.containsKey(ignoreSpans.endPosition())) {
            baseStartPositionList.put(ignoreSpans.endPosition(),
                new HashSet<Integer>());
            minBaseStartPosition.put(ignoreSpans.endPosition(),
                ignoreSpans.endPosition());
          } else {
            minBaseStartPosition.put(ignoreSpans.endPosition(),
                Math.min(minBaseStartPosition.get(ignoreSpans.endPosition()),
                    currentPosition));
          }
          baseStartPositionList.get(ignoreSpans.endPosition())
              .add(currentPosition);
          baseEndPositionList.get(currentPosition)
              .add(ignoreSpans.endPosition());
        }
      } catch (IOException e) {
        log.debug(e);
        currentPosition = Spans.NO_MORE_POSITIONS;
        break;
      }
    }
  }
163045
public function seek($position)
    {
        reset($this->_storage);
        while (($key = key($this->_storage)) !== null && $key !== $position)
        {
            next($this->_storage);
        }
    }
479166
public function afterDelete(Model &$model)
    {
        /** @var \T4\Orm\Model $class */
        $class = get_class($model);
        $tableName = $class::getTableName();
        /** @var \T4\Dbal\Connection $connection */
        $connection = $class::getDbConnection();

        $query = (new Query())
            ->delete()
            ->table($tableName)
            ->where('__lft>:lft AND __rgt<:rgt')
            ->params([':lft' => $model->__lft, ':rgt' => $model->__rgt]);
        $connection->execute($query);

        $this->removeFromTreeByLftRgt($connection, $tableName, $model->__lft, $model->__rgt);

        $model->__lft = 0;
        $model->__rgt = 0;
        $model->__lvl = 0;
        $model->__prt = 0;
    }
981658
protected function doSetSkillId(Position $model, $relatedId) {
		if ($model->getEndPositionId() !== $relatedId) {
			$model->setEndPositionId($relatedId);

			return true;
		}

		return false;
	}
1213748
public function flush($model)
    {
        if (! $this->shouldMaintainIndex($model)) {
            return;
        }

        $indexColumn = $this->getIndexColumn($model);

        $this->database
            ->table($model->searchableAs())
            ->update([$indexColumn => null]);
    }