diff --git a/CHANGELOG.md b/CHANGELOG.md index 05b1d63936a..12e548dcd9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # [3.1.2](https://github.com/phalcon/cphalcon/releases/tag/v3.1.2) (2017-XX-XX) - Fixed PHP 7.1 issues [#12055](https://github.com/phalcon/cphalcon/issues/12055) - Fixed `Imagick::getVersion()` error in some system [#12729](https://github.com/phalcon/cphalcon/pull/12729) +- Fixed `Phalcon\Mvc\Model::_doLowInsert` to properly set snapshot when having default values and public properties [#12766](https://github.com/phalcon/cphalcon/issues/12766) # [3.1.1](https://github.com/phalcon/cphalcon/releases/tag/v3.1.1) (2017-03-25) - Fixed undefined index warning on existing cached resultsets diff --git a/phalcon/mvc/model.zep b/phalcon/mvc/model.zep index f7bcde283dc..ca7c718613c 100644 --- a/phalcon/mvc/model.zep +++ b/phalcon/mvc/model.zep @@ -2290,7 +2290,10 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface if fetch value, this->{attributeField} { if value === null && isset defaultValues[field] { + let snapshot[attributeField] = null; let value = connection->getDefaultValue(); + } else { + let snapshot[attributeField] = value; } /** @@ -2301,7 +2304,6 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface } let fields[] = field, values[] = value, bindTypes[] = bindType; - let snapshot[attributeField] = value; } else { if isset defaultValues[field] { diff --git a/tests/_data/models/DynamicUpdate/Robots.php b/tests/_data/models/DynamicUpdate/Robots.php new file mode 100644 index 00000000000..e25f5f701cd --- /dev/null +++ b/tests/_data/models/DynamicUpdate/Robots.php @@ -0,0 +1,45 @@ + + * @author Serghei Iakovlev + * @package Phalcon\Test\Models\DynamicUpdate + * + * @property int $id + * @property string $name + * @property string $type + * @property int $year + * @property string $datetime + * @property string $deleted + * @property string $text + * + * @method static Robots findFirst($parameters = null) + * @method static Robots[] find($parameters = null) + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file docs/LICENSE.txt + * + * If you did not receive a copy of the license and are unable to obtain it + * through the world-wide-web, please send an email to license@phalconphp.com + * so that we can send you a copy immediately. + */ +class Robots extends Model +{ + public $year; + + public function initialize() + { + $this->hasMany('id', RobotsParts::class, 'robots_id'); + + $this->useDynamicUpdate(true); + } +} diff --git a/tests/unit/Mvc/Model/DynamicUpdateTest.php b/tests/unit/Mvc/Model/DynamicUpdateTest.php new file mode 100644 index 00000000000..a02617e5c6c --- /dev/null +++ b/tests/unit/Mvc/Model/DynamicUpdateTest.php @@ -0,0 +1,48 @@ + + * @author Serghei Iakovlev + * @author Wojciech Ślawski + * @package Phalcon\Test\Unit\Mvc\Model + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file docs/LICENSE.txt + * + * If you did not receive a copy of the license and are unable to obtain it + * through the world-wide-web, please send an email to license@phalconphp.com + * so that we can send you a copy immediately. + */ +class DynamicUpdateTest extends UnitTest +{ + /** + * Tests dynamic update create then update + * + * @author Wojciech Ślawski + * @issue 12766 + * @since 2017-04-04 + */ + public function testIssue12766() + { + $robots = new Robots(); + $robots->name = 'Test'; + $robots->type = 'mechanical'; + $robots->datetime = (new \DateTime())->format('Y-m-d'); + $robots->text = 'text'; + + $robots->create(); + + $robots->year = 2017; + $robots->update(); + } +}