Skip to content

Commit

Permalink
Move blank primary key filtering to the marshaller.
Browse files Browse the repository at this point in the history
I agree with jose_zap that having it here makes more sense, as the
marshaller is responsible for converting request data into sensical
entities.
  • Loading branch information
markstory committed Aug 12, 2014
1 parent 90a7016 commit fe2846f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/ORM/Marshaller.php
Expand Up @@ -106,12 +106,16 @@ public function one(array $data, array $options = []) {
$data = $data[$tableName];
}

$primaryKey = $schema->primaryKey();
$properties = [];
foreach ($data as $key => $value) {
$columnType = $schema->columnType($key);
if (isset($propertyMap[$key])) {
$assoc = $propertyMap[$key]['association'];
$value = $this->_marshalAssociation($assoc, $value, $propertyMap[$key]);
} elseif ($value === '' && in_array($key, $primaryKey, true)) {
// Skip marshalling '' for pk fields.
continue;
} elseif ($columnType) {
$converter = Type::build($columnType);
$value = $converter->marshal($value);
Expand Down
3 changes: 0 additions & 3 deletions src/ORM/Table.php
Expand Up @@ -1152,9 +1152,6 @@ protected function _processSave($entity, $options) {
$alias = $this->alias();
$conditions = [];
foreach ($primary as $k => $v) {
if ($v === '') {
$entity->unsetProperty($k);
}
$conditions["$alias.$k"] = $v;
}
$entity->isNew(!$this->exists($conditions));
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -114,6 +114,27 @@ public function testOneSimple() {
$this->assertEquals('Articles', $result->source());
}

/**
* Test that marshalling an entity with '' for pk values results
* in no pk value being set.
*
* @return void
*/
public function testOneEmptyStringPrimaryKey() {
$data = [
'id' => '',
'username' => 'superuser',
'password' => 'root',
'created' => new Time('2013-10-10 00:00'),
'updated' => new Time('2013-10-10 00:00')
];
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, []);

$this->assertFalse($result->dirty('id'));
$this->assertNull($result->id);
}

/**
* Test marshalling datetime/date field.
*
Expand Down
20 changes: 0 additions & 20 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -1212,26 +1212,6 @@ public function testSaveNewEmptyEntity() {
$this->assertFalse($table->save($entity));
}

/**
* Test that saving a new entity with an empty id populates
* the entity's id field.
*
* @group save
* @return void
*/
public function testSaveNewEntityEmptyIdField() {
$entity = new \Cake\ORM\Entity([
'id' => '',
'username' => 'superuser',
'password' => 'root',
'created' => new Time('2013-10-10 00:00'),
'updated' => new Time('2013-10-10 00:00')
]);
$table = TableRegistry::get('users');
$this->assertSame($entity, $table->save($entity));
$this->assertEquals($entity->id, self::$nextUserId);
}

/**
* Tests that saving an entity will filter out properties that
* are not present in the table schema when saving
Expand Down

0 comments on commit fe2846f

Please sign in to comment.