Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix model field defaults to an empty string and is determined to be null #12507

Merged
merged 3 commits into from
Dec 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/Mvc/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
);
}
}