Ordering objects with sortable behavior in Doctrine 2 (Symfony 2)

I created an object that has a Sortable and you have one question about using it.
The methods of setting up and getting positions are not enough for me, so I want to make simple moveUp and moveDown with the following code:

public function moveUp() {
    ++$this->position;
}

public function moveDown() {
    if($this->position != 0)
        --$this->position;
}

In this implementation, the moveUp method has no restrictions for increasing an element with already max. position. What is the best way to prohibit the increase of such items? I heard that doing custom queries directly in essence is not good practice, so how can I check if an element already has a maximum value?

+5
2

, , , :

// Compute position if it is negative
if ($newPosition < 0) {
    $newPosition += $this->maxPositions[$hash] + 2; // position == -1 => append at end of list
    if ($newPosition < 0) $newPosition = 0;
}

( SortableListener: 141)

, , -1 , . -2 .


: , , :

, , SortableListener :

// Set position to max position if it is too big
$newPosition = min(array($this->maxPositions[$hash] + 1, $newPosition));

, moveUp() (, moveUp moveDown?):

public function moveUp()
{
    if ($this->position != 0) {
        $this->position--;
    }
}
public function moveDown()
{
    $this->position++;
}

: , .

setPosition(-1) , .

: https://github.com/l3pp4rd/DoctrineExtensions/pull/908

, .

+2

, , SortableMaxAware d , . , , , , ORM:) .

0

All Articles