- $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
- $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
- $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
- $model->priority = $index;
- if ($isPage) {
- $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
+ $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
+ $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
+ $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
+
+ // Update models only if there's a change in parent chain or ordering.
+ if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
+ $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
+ $model->priority = $priority;
+ if ($isPage) $model->chapter_id = $chapterId;
+ $model->save();
+ $updatedModels->push($model);