Remove primary key CollectionEbookId

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.
This commit is contained in:
Mike Colagrosso 2024-10-13 20:30:07 -06:00 committed by Alex Cabal
parent 4a7483411a
commit cada95383f
3 changed files with 9 additions and 9 deletions

View file

@ -1,8 +1,7 @@
CREATE TABLE `CollectionEbooks` ( CREATE TABLE `CollectionEbooks` (
`CollectionEbookId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`EbookId` int(10) unsigned NOT NULL, `EbookId` int(10) unsigned NOT NULL,
`CollectionId` int(10) unsigned NOT NULL, `CollectionId` int(10) unsigned NOT NULL,
`SequenceNumber` int(10) unsigned NULL, `SequenceNumber` int(10) unsigned NULL,
PRIMARY KEY (`CollectionEbookId`), `SortOrder` tinyint(3) unsigned NOT NULL,
UNIQUE KEY `idxUnique` (`EbookId`,`CollectionId`) UNIQUE KEY `idxUnique` (`EbookId`,`CollectionId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

View file

@ -5,9 +5,9 @@
class CollectionMembership{ class CollectionMembership{
use Traits\Accessor; use Traits\Accessor;
public ?int $CollectionEbookId = null;
public ?int $EbookId = null; public ?int $EbookId = null;
public ?int $CollectionId = null; public ?int $CollectionId = null;
public ?int $SequenceNumber = null; public ?int $SequenceNumber = null;
public ?int $SortOrder = null;
protected ?Collection $_Collection = null; protected ?Collection $_Collection = null;
} }

View file

@ -177,7 +177,7 @@ class Ebook{
SELECT * SELECT *
from CollectionEbooks from CollectionEbooks
where EbookId = ? where EbookId = ?
order by CollectionEbookId order by SortOrder asc
', [$this->EbookId], CollectionMembership::class); ', [$this->EbookId], CollectionMembership::class);
} }
@ -1737,19 +1737,20 @@ class Ebook{
} }
private function AddCollectionMemberships(): void{ private function AddCollectionMemberships(): void{
foreach($this->CollectionMemberships as $collectionMembership){ foreach($this->CollectionMemberships as $sortOrder => $collectionMembership){
$collectionMembership->EbookId = $this->EbookId; $collectionMembership->EbookId = $this->EbookId;
$collectionMembership->CollectionId = $collectionMembership->Collection->CollectionId; $collectionMembership->CollectionId = $collectionMembership->Collection->CollectionId;
$collectionMembership->SortOrder = $sortOrder;
try{ try{
Db::Query(' Db::Query('
INSERT into CollectionEbooks (EbookId, CollectionId, SequenceNumber) INSERT into CollectionEbooks (EbookId, CollectionId, SequenceNumber, SortOrder)
values (?, values (?,
?,
?, ?,
?) ?)
', [$collectionMembership->EbookId, $collectionMembership->CollectionId, $collectionMembership->SequenceNumber]); ', [$collectionMembership->EbookId, $collectionMembership->CollectionId, $collectionMembership->SequenceNumber,
$collectionMembership->SortOrder]);
$collectionMembership->CollectionEbookId = Db::GetLastInsertedId();
} }
catch(Exceptions\DuplicateDatabaseKeyException){ catch(Exceptions\DuplicateDatabaseKeyException){
// The Ebook is already a member of this Collection. // The Ebook is already a member of this Collection.