From cada95383fd5ef1198dae0f5ec2b93cc683acd18 Mon Sep 17 00:00:00 2001 From: Mike Colagrosso Date: Sun, 13 Oct 2024 20:30:07 -0600 Subject: [PATCH] Remove primary key CollectionEbookId MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a SortOrder to preserve the order of the collections listed in content.opf for ebook.php, e.g., for To the Lighthouse: № 15 in the Modern Library’s 100 Best Novels set. Part of the Encyclopædia Britannica’s Great Books of the Western World set. № 2 in the BBC’s 100 Greatest British Novels (2015) set. --- config/sql/se/CollectionEbooks.sql | 3 +-- lib/CollectionMembership.php | 2 +- lib/Ebook.php | 13 +++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/config/sql/se/CollectionEbooks.sql b/config/sql/se/CollectionEbooks.sql index 321dd8b5..9615d995 100644 --- a/config/sql/se/CollectionEbooks.sql +++ b/config/sql/se/CollectionEbooks.sql @@ -1,8 +1,7 @@ CREATE TABLE `CollectionEbooks` ( - `CollectionEbookId` int(10) unsigned NOT NULL AUTO_INCREMENT, `EbookId` int(10) unsigned NOT NULL, `CollectionId` int(10) unsigned NOT NULL, `SequenceNumber` int(10) unsigned NULL, - PRIMARY KEY (`CollectionEbookId`), + `SortOrder` tinyint(3) unsigned NOT NULL, UNIQUE KEY `idxUnique` (`EbookId`,`CollectionId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/lib/CollectionMembership.php b/lib/CollectionMembership.php index e5e8f4f6..8df0b276 100644 --- a/lib/CollectionMembership.php +++ b/lib/CollectionMembership.php @@ -5,9 +5,9 @@ class CollectionMembership{ use Traits\Accessor; - public ?int $CollectionEbookId = null; public ?int $EbookId = null; public ?int $CollectionId = null; public ?int $SequenceNumber = null; + public ?int $SortOrder = null; protected ?Collection $_Collection = null; } diff --git a/lib/Ebook.php b/lib/Ebook.php index 8f8136d9..439790b8 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -177,7 +177,7 @@ class Ebook{ SELECT * from CollectionEbooks where EbookId = ? - order by CollectionEbookId + order by SortOrder asc ', [$this->EbookId], CollectionMembership::class); } @@ -1737,19 +1737,20 @@ class Ebook{ } private function AddCollectionMemberships(): void{ - foreach($this->CollectionMemberships as $collectionMembership){ + foreach($this->CollectionMemberships as $sortOrder => $collectionMembership){ $collectionMembership->EbookId = $this->EbookId; $collectionMembership->CollectionId = $collectionMembership->Collection->CollectionId; + $collectionMembership->SortOrder = $sortOrder; try{ Db::Query(' - INSERT into CollectionEbooks (EbookId, CollectionId, SequenceNumber) + INSERT into CollectionEbooks (EbookId, CollectionId, SequenceNumber, SortOrder) values (?, + ?, ?, ?) - ', [$collectionMembership->EbookId, $collectionMembership->CollectionId, $collectionMembership->SequenceNumber]); - - $collectionMembership->CollectionEbookId = Db::GetLastInsertedId(); + ', [$collectionMembership->EbookId, $collectionMembership->CollectionId, $collectionMembership->SequenceNumber, + $collectionMembership->SortOrder]); } catch(Exceptions\DuplicateDatabaseKeyException){ // The Ebook is already a member of this Collection.