Skip to content
Permalink
Browse files

Consistently read the PK from the table (instead of the schema).

  • Loading branch information...
ndm2
ndm2 committed May 31, 2016
1 parent 445e0cc commit df3479a42643c2e3ee3a81ba47cd0767d3c9d5ca
Showing with 56 additions and 2 deletions.
  1. +2 −2 src/ORM/Marshaller.php
  2. +54 −0 tests/TestCase/ORM/MarshallerTest.php
@@ -119,7 +119,7 @@ public function one(array $data, array $options = [])
$propertyMap = $this->_buildPropertyMap($options);
$schema = $this->_table->schema();
$primaryKey = $schema->primaryKey();
$primaryKey = (array)$this->_table->primaryKey();
$entityClass = $this->_table->entityClass();
$entity = new $entityClass();
$entity->source($this->_table->registryAlias());
@@ -314,7 +314,7 @@ protected function _belongsToMany(Association $assoc, array $data, $options = []
$data = array_values($data);
$target = $assoc->target();
$primaryKey = array_flip($target->schema()->primaryKey());
$primaryKey = array_flip((array)$target->primaryKey());
$records = $conditions = [];
$primaryCount = count($primaryKey);
$conditions = [];
@@ -2923,4 +2923,58 @@ public function testAssociationNoChanges()
$this->assertFalse($entity->dirty('user'));
$this->assertTrue($entity->user->isNew());
}
public function testEnsurePrimaryKeyBeingReadFromTableForHandlingEmptyStringPrimaryKey()
{
$data = [
'id' => ''
];
$articles = TableRegistry::get('Articles');
$articles->schema()->dropConstraint('primary');
$articles->primaryKey('id');
$marshall = new Marshaller($articles);
$result = $marshall->one($data);
$this->assertFalse($result->dirty('id'));
$this->assertNull($result->id);
}
public function testEnsurePrimaryKeyBeingReadFromTableWhenLoadingBelongsToManyRecordsByPrimaryKey()
{
$data = [
'tags' => [
[
'id' => 1
],
[
'id' => 2
]
]
];
$tags = TableRegistry::get('Tags');
$tags->schema()->dropConstraint('primary');
$tags->primaryKey('id');
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, ['associated' => ['Tags']]);
$expected = [
'tags' => [
[
'id' => 1,
'name' => 'tag1',
'description' => 'A big description'
],
[
'id' => 2,
'name' => 'tag2',
'description' => 'Another big description'
]
]
];
$this->assertEquals($expected, $result->toArray());
}
}

0 comments on commit df3479a

Please sign in to comment.
You can’t perform that action at this time.