From 0ad69cbb37ea62c20b75461cdbb79c020e56136d Mon Sep 17 00:00:00 2001 From: Oscar Otero Date: Thu, 12 Dec 2019 16:39:36 +0100 Subject: [PATCH] fixed null values in Json and Serializable fields --- CHANGELOG.md | 6 +++++- src/Fields/Json.php | 4 ++++ src/Fields/Serializable.php | 4 ++++ tests/JsonFieldTest.php | 4 ---- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b486c4..7ce15ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## Unreleased +## [7.3.5] - 2019-12-12 ### Added - New function `getArray()` to SELECT queries, to return an array with data instead a `Row` or `RowCollection`. +### Fixed +- `NULL` values in `Json` and `Serializable` fields + ## [7.3.4] - 2019-12-03 ### Fixed - Revert the bugfix added in v7.3.0 about not including `NULL` values on insert and fixed in a different way. @@ -92,6 +95,7 @@ This library was rewritten and a lot of breaking changes were included. - The pagination info is returned with `$selectQuery->getPageInfo()` function. - Many other changes. See the docs. +[7.3.5]: https://github.com/oscarotero/simple-crud/compare/v7.3.4...v7.3.5 [7.3.4]: https://github.com/oscarotero/simple-crud/compare/v7.3.3...v7.3.4 [7.3.3]: https://github.com/oscarotero/simple-crud/compare/v7.3.2...v7.3.3 [7.3.2]: https://github.com/oscarotero/simple-crud/compare/v7.3.1...v7.3.2 diff --git a/src/Fields/Json.php b/src/Fields/Json.php index fcb0011..ea4c10e 100644 --- a/src/Fields/Json.php +++ b/src/Fields/Json.php @@ -24,6 +24,10 @@ public function format($value) protected function formatToDatabase($value): ?string { + if ($value === null) { + return $value; + } + if (!is_string($value)) { return json_encode($value) ?: null; } diff --git a/src/Fields/Serializable.php b/src/Fields/Serializable.php index c0493c0..23e95f9 100644 --- a/src/Fields/Serializable.php +++ b/src/Fields/Serializable.php @@ -25,6 +25,10 @@ public function format($value) protected function formatToDatabase($value): string { + if ($value === null) { + return $value; + } + return serialize($value); } } diff --git a/tests/JsonFieldTest.php b/tests/JsonFieldTest.php index 30fc6ff..bde7d20 100644 --- a/tests/JsonFieldTest.php +++ b/tests/JsonFieldTest.php @@ -34,9 +34,5 @@ public function testFields() $post = $db->post[1]; $this->assertNull($post->info); - - $array = $db->post->select()->one()->getArray(); - - print_r($array); } }