From 10aa3ed12ab994e96168cc3c515e4ced6785a042 Mon Sep 17 00:00:00 2001 From: oscarotero Date: Sat, 15 Feb 2014 19:02:30 +0100 Subject: [PATCH] Fixed a bug saving data modified by dataToDatabase --- SimpleCrud/Entity.php | 6 ++++-- tests/SimpleCrudTest.php | 14 ++++++++++++++ tests/db.sql | 7 +++++++ tests/entities.php | 8 ++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/SimpleCrud/Entity.php b/SimpleCrud/Entity.php index 0a8cbb6..45e118c 100644 --- a/SimpleCrud/Entity.php +++ b/SimpleCrud/Entity.php @@ -533,10 +533,11 @@ private function filterDataToSave (array $data, array $prepared, array $changedF * @return array The new values of the inserted row */ public function insert (array $data, $duplicateKey = false, array $changedFields = null) { + $originalData = $data; $preparedData = $this->prepareDataToDatabase($data, true); if ($changedFields !== null) { - $preparedData = $this->filterDataToSave($data, $preparedData, $changedFields); + $preparedData = $this->filterDataToSave($originalData, $preparedData, $changedFields); } unset($preparedData['id']); @@ -586,10 +587,11 @@ public function insert (array $data, $duplicateKey = false, array $changedFields * @return array The new values of the updated row */ public function update (array $data, $where = '', $marks = null, $limit = null, array $changedFields = null) { + $originalData = $data; $preparedData = $this->prepareDataToDatabase($data, true); if ($changedFields !== null) { - $preparedData = $this->filterDataToSave($data, $preparedData, $changedFields); + $preparedData = $this->filterDataToSave($originalData, $preparedData, $changedFields); } unset($preparedData['id']); diff --git a/tests/SimpleCrudTest.php b/tests/SimpleCrudTest.php index 1637a2c..21cc85c 100644 --- a/tests/SimpleCrudTest.php +++ b/tests/SimpleCrudTest.php @@ -190,6 +190,20 @@ public function testRelations () { $this->assertEquals(['1', '2'], $tags->id); } + public function testDataToDatabase () { + $db = self::$db; + + $row = $db->testing->create(['field1' => 'hello'])->save(); + $this->assertSame($row->field1, $row->field2); + + + $row->reload(); + $this->assertSame($row->field1, $row->field2); + + $row->set(['field1' => 'bye'])->save(); + $this->assertSame($row->field1, $row->field2); + } + public function testDatetimeFields () { $db = self::$db; diff --git a/tests/db.sql b/tests/db.sql index f020aaa..179602a 100644 --- a/tests/db.sql +++ b/tests/db.sql @@ -65,6 +65,13 @@ CREATE TABLE `customfield` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `testing` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `field1` varchar(255) DEFAULT NULL, + `field2` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + -- -- Restricións para os envorcados das táboas -- diff --git a/tests/entities.php b/tests/entities.php index 5c32e29..2b44723 100644 --- a/tests/entities.php +++ b/tests/entities.php @@ -6,6 +6,14 @@ class CustomField extends \SimpleCrud\Entity { 'field' => 'json' ]; } + + class Testing extends \SimpleCrud\Entity { + public function dataToDatabase (array $data, $new) { + $data['field2'] = $data['field1']; + + return $data; + } + } } namespace CustomEntities\Fields {