/** * @param \Mirasvit\Blog\Model\Category $category * @param \Mirasvit\Blog\Model\Category $newParent * @param null|int $afterCategoryId * @return int */ protected function processPositions($category, $newParent, $afterCategoryId) { $table = $this->getEntityTable(); $connection = $this->getConnection(); $positionField = $connection->quoteIdentifier('position'); $bind = ['position' => new \Zend_Db_Expr($positionField . ' - 1')]; $where = ['parent_id = ?' => $category->getParentId(), $positionField . ' > ?' => $category->getPosition()]; $connection->update($table, $bind, $where); /** * Prepare position value */ if ($afterCategoryId) { $select = $connection->select()->from($table, 'position')->where('entity_id = :entity_id'); $position = $connection->fetchOne($select, ['entity_id' => $afterCategoryId]); $position += 1; } else { $position = 1; } $bind = ['position' => new \Zend_Db_Expr($positionField . ' + 1')]; $where = ['parent_id = ?' => $newParent->getId(), $positionField . ' >= ?' => $position]; $connection->update($table, $bind, $where); return $position; }