From 6d3aab5af08e154850461922d27127f6830e5da0 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Thu, 8 Aug 2024 11:35:51 +0200 Subject: [PATCH] Database fixes on catalogus and publication --- lib/Db/Catalog.php | 12 ++- lib/Db/Publication.php | 9 ++- lib/Migration/Version6Date20240723125106.php | 6 +- lib/Migration/Version6Date20240808085441.php | 77 ++++++++++++++++++++ lib/Migration/Version6Date20240808092738.php | 63 ++++++++++++++++ lib/Migration/Version6Date20240808093230.php | 62 ++++++++++++++++ 6 files changed, 222 insertions(+), 7 deletions(-) create mode 100644 lib/Migration/Version6Date20240808085441.php create mode 100644 lib/Migration/Version6Date20240808092738.php create mode 100644 lib/Migration/Version6Date20240808093230.php diff --git a/lib/Db/Catalog.php b/lib/Db/Catalog.php index 00ee3c38..05958101 100644 --- a/lib/Db/Catalog.php +++ b/lib/Db/Catalog.php @@ -17,7 +17,7 @@ class Catalog extends Entity implements JsonSerializable protected bool $listed = false; protected ?string $organisation = null; - protected array $metadata = []; + protected ?array $metadata = null; public function __construct() { $this->addType(fieldName: 'title', type: 'string'); @@ -42,11 +42,17 @@ public function getJsonFields(): array public function hydrate(array $object): self { + + + if(isset($object['metadata']) === false) { + $object['metadata'] = []; + } + $jsonFields = $this->getJsonFields(); foreach($object as $key => $value) { if (in_array($key, $jsonFields) === true && $value === []) { - $value = null; + $value = []; } $method = 'set'.ucfirst($key); @@ -71,6 +77,8 @@ public function jsonSerialize(): array 'image' => $this->image, 'search' => $this->search, 'listed' => $this->listed, + 'metadata' => $this->metadata, + 'organisation'=> $this->organisation, ]; diff --git a/lib/Db/Publication.php b/lib/Db/Publication.php index fdfb177d..9ea2a93b 100644 --- a/lib/Db/Publication.php +++ b/lib/Db/Publication.php @@ -77,6 +77,11 @@ public function hydrate(array $object): self $this->setData(null); $this->setModified(new DateTime()); + + if(isset($object['published']) === false) { + $object['published'] = null; + } + foreach($object as $key => $value) { if (in_array($key, $jsonFields) === true && $value === []) { $value = []; @@ -114,8 +119,8 @@ public function jsonSerialize(): array 'portal' => $this->portal, 'catalogi' => $this->catalogi, 'metaData' => $this->metaData, - 'published' => $this->published->format('c'), - 'modified' => $this->modified->format('c'), + 'published' => $this->published?->format('c'), + 'modified' => $this->modified?->format('c'), 'featured' => $this->featured !== null ? (bool) $this->featured : null, 'organization' => $this->organization, 'data' => $this->data, diff --git a/lib/Migration/Version6Date20240723125106.php b/lib/Migration/Version6Date20240723125106.php index 4e18cd45..d12ccc8d 100644 --- a/lib/Migration/Version6Date20240723125106.php +++ b/lib/Migration/Version6Date20240723125106.php @@ -81,15 +81,15 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt ] ); $table->addColumn(name: 'organization', typeName: TYPES::JSON, options: [ - 'default' => [], + 'default' => 'a:0:{}', 'notnull' => false, ]); $table->addColumn(name: 'data', typeName: TYPES::JSON, options: [ - 'default' => [], + 'default' => 'a:0:{}', 'notnull' => false, ]); $table->addColumn(name: 'attachments', typeName: TYPES::JSON, options: [ - 'default' => [], + 'default' => 'a:0:{}', 'notnull' => false, ]); $table->addColumn(name: 'attachment_count', typeName: TYPES::INTEGER); diff --git a/lib/Migration/Version6Date20240808085441.php b/lib/Migration/Version6Date20240808085441.php new file mode 100644 index 00000000..28e43b3a --- /dev/null +++ b/lib/Migration/Version6Date20240808085441.php @@ -0,0 +1,77 @@ +hasTable(tableName: 'catalogi') === true) { + $table = $schema->getTable(tableName: 'catalogi'); + + if($table->hasColumn(name: 'organization') === false) { + $table->addColumn( + name: 'organization', + typeName: Types::STRING, + options: [ + 'notNull' => false, + 'default' => null + ]); + } + if($table->hasColumn(name: 'metadata') === false) { + $table->addColumn( + name: 'metadata', + typeName: Types::JSON, + options: [ + 'notNull' => false, + 'default' => 'a:0:{}' + ]); + } + + } + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } +} diff --git a/lib/Migration/Version6Date20240808092738.php b/lib/Migration/Version6Date20240808092738.php new file mode 100644 index 00000000..e5babbe7 --- /dev/null +++ b/lib/Migration/Version6Date20240808092738.php @@ -0,0 +1,63 @@ +hasTable(tableName: 'publications') === true) { + $table = $schema->getTable(tableName: 'publications'); + + if($table->hasColumn(name: 'published') === true) { + $column = $table->getColumn(name: 'published'); + $column->setDefault(default: null); + } + + } + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } +} diff --git a/lib/Migration/Version6Date20240808093230.php b/lib/Migration/Version6Date20240808093230.php new file mode 100644 index 00000000..81e9d2bc --- /dev/null +++ b/lib/Migration/Version6Date20240808093230.php @@ -0,0 +1,62 @@ +hasTable(tableName: 'publications') === true) { + $table = $schema->getTable(tableName: 'publications'); + + if($table->hasColumn(name: 'published') === true) { + $column = $table->getColumn(name: 'published'); + $column->setNotnull(notnull: false); + } + + } + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } +}