datagrid
datagrid copied to clipboard
Documentation > Sorting
Hello guys,
I found a mistake in the example source code of sorting. When you will move a penultimate record and put it behind the last record, the order will crash.
The problem is in the part 3. When the next item does not exist, there is inserted zero and all records greater or equal to zero are increased. Correctly is number of records minus one.

/**
* 3, Find all items that have to be moved one position down
*/
$itemsToMoveDown = $repository->createQueryBuilder('r')
->where('r.order >= :order')
->setParameter('order', $nextItem ? $nextItem->getOrder() : 0)
->andWhere('r.order < :order2')
->setParameter('order2', $item->getOrder())
->getQuery()
->getResult();
foreach ($itemsToMoveDown as $t) {
$t->setOrder($t->getOrder() + 1);
$this->em->persist($t);
}