Add check for DuplicateDatabaseKeyException

when inserting into join tables EbookTags, EbookLocSubjects, CollectionEbooks
This commit is contained in:
Mike Colagrosso 2024-10-12 16:42:17 -06:00 committed by Alex Cabal
parent 38860c4729
commit d0926370be

View file

@ -1692,12 +1692,17 @@ class Ebook{
private function AddTags(): void{ private function AddTags(): void{
foreach($this->Tags as $tag){ foreach($this->Tags as $tag){
try{
Db::Query(' Db::Query('
INSERT into EbookTags (EbookId, TagId) INSERT into EbookTags (EbookId, TagId)
values (?, values (?,
?) ?)
', [$this->EbookId, $tag->TagId]); ', [$this->EbookId, $tag->TagId]);
} }
catch(Exceptions\DuplicateDatabaseKeyException){
// The Ebook already has the Tag, which is fine.
}
}
} }
private function RemoveLocSubjects(): void{ private function RemoveLocSubjects(): void{
@ -1710,12 +1715,17 @@ class Ebook{
private function AddLocSubjects(): void{ private function AddLocSubjects(): void{
foreach($this->LocSubjects as $locSubject){ foreach($this->LocSubjects as $locSubject){
try{
Db::Query(' Db::Query('
INSERT into EbookLocSubjects (EbookId, LocSubjectId) INSERT into EbookLocSubjects (EbookId, LocSubjectId)
values (?, values (?,
?) ?)
', [$this->EbookId, $locSubject->LocSubjectId]); ', [$this->EbookId, $locSubject->LocSubjectId]);
} }
catch(Exceptions\DuplicateDatabaseKeyException){
// The Ebook already has the LocSubject, which is fine.
}
}
} }
private function RemoveCollectionMemberships(): void{ private function RemoveCollectionMemberships(): void{
@ -1731,6 +1741,7 @@ class Ebook{
$collectionMembership->EbookId = $this->EbookId; $collectionMembership->EbookId = $this->EbookId;
$collectionMembership->CollectionId = $collectionMembership->Collection->CollectionId; $collectionMembership->CollectionId = $collectionMembership->Collection->CollectionId;
try{
Db::Query(' Db::Query('
INSERT into CollectionEbooks (EbookId, CollectionId, SequenceNumber) INSERT into CollectionEbooks (EbookId, CollectionId, SequenceNumber)
values (?, values (?,
@ -1740,6 +1751,10 @@ class Ebook{
$collectionMembership->CollectionEbookId = Db::GetLastInsertedId(); $collectionMembership->CollectionEbookId = Db::GetLastInsertedId();
} }
catch(Exceptions\DuplicateDatabaseKeyException){
// The Ebook is already a member of this Collection.
}
}
} }
private function RemoveGitCommits(): void{ private function RemoveGitCommits(): void{