From 6b278166f6b27225237d95294c23ce3f5afbbf55 Mon Sep 17 00:00:00 2001 From: gavin Date: Mon, 26 Dec 2016 16:54:21 +0800 Subject: [PATCH 1/3] fix model field defaults to an empty string and is determined to be null --- phalcon/mvc/model.zep | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phalcon/mvc/model.zep b/phalcon/mvc/model.zep index 8670297d182..c67322a9342 100644 --- a/phalcon/mvc/model.zep +++ b/phalcon/mvc/model.zep @@ -2045,8 +2045,8 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface let automaticAttributes = metaData->getAutomaticUpdateAttributes(this); } else { let automaticAttributes = metaData->getAutomaticCreateAttributes(this); - let defaultValues = metaData->getDefaultValues(this); } + let defaultValues = metaData->getDefaultValues(this); /** * Get string attributes that allow empty strings as defaults @@ -2088,7 +2088,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface let isNull = true; } } else { - if value === null || value === "" { + if value === null || (value === "" && value !== defaultValues[field]) { let isNull = true; } } From 2e33bf7858cc8508d3d7d10a7821955d7b00df81 Mon Sep 17 00:00:00 2001 From: gavin Date: Mon, 26 Dec 2016 19:07:09 +0800 Subject: [PATCH 2/3] added unit test for #12507 --- tests/unit/Mvc/ModelTest.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/unit/Mvc/ModelTest.php b/tests/unit/Mvc/ModelTest.php index 5ecf461bff1..4063b8c2f83 100644 --- a/tests/unit/Mvc/ModelTest.php +++ b/tests/unit/Mvc/ModelTest.php @@ -2,6 +2,7 @@ namespace Phalcon\Test\Unit\Mvc; +use Phalcon\Test\Models\Personers; use Phalcon\Test\Models\Users; use Phalcon\Cache\Backend\Apc; use Phalcon\Test\Models\Robots; @@ -754,4 +755,38 @@ function () { } ); } + + /** + * for PR #12507 + */ + public function testFieldDefaultEmptyStringIsNull() + { + $this->specify( + 'The field default value is empty string and is determined to be null', + function () { + $personers = new Personers(); + $personers->borgerId = 'id-' . time() . rand(1, 99); + $personers->slagBorgerId = 1; + $personers->kredit = 2.3; + $personers->status = 'A'; + + // test field for create + $personers->navnes = ''; + $created = $personers->create(); + expect($created)->true(); + + // write something to not null default '' field + $personers->navnes = 'save something!'; + $saved = $personers->save(); + expect($saved)->true(); + + // test field for update + $personers->navnes = ''; + $saved = $personers->save(); + expect($saved)->true(); + + $personers->delete(); + } + ); + } } From f8d699a5109da89e7856d463336aab7d4110c8c3 Mon Sep 17 00:00:00 2001 From: gavin Date: Wed, 28 Dec 2016 15:46:07 +0800 Subject: [PATCH 3/3] update CHANGELOG.md [ci skip] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ed9fe29a63..8447791177e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # [3.0.4](https://github.com/phalcon/cphalcon/releases/tag/v3.0.4) (XXXX-XX-XX) +- Fixed Isnull check is not correct when the model field defaults to an empty string. [#12507](https://github.com/phalcon/cphalcon/issues/12507) # [3.0.3](https://github.com/phalcon/cphalcon/releases/tag/v3.0.3) (2016-12-24) - Fixed implementation of Iterator interface in a `Phalcon\Forms\Form` that could cause a run-time warning