Skip to content

Commit

Permalink
Fixed a bug saving data modified by dataToDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Feb 15, 2014
1 parent 1d47b47 commit 10aa3ed
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions SimpleCrud/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down
14 changes: 14 additions & 0 deletions tests/SimpleCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions tests/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down
8 changes: 8 additions & 0 deletions tests/entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 10aa3ed

Please sign in to comment.