Skip to content

Commit

Permalink
Passing the primary key values to the validation data, as it can
Browse files Browse the repository at this point in the history
be useful for certain validation routines.
  • Loading branch information
lorenzo committed Dec 31, 2014
1 parent 5bbd189 commit f5cd576
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/ORM/Marshaller.php
Expand Up @@ -317,12 +317,18 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
$options += ['validate' => true];
$propertyMap = $this->_buildPropertyMap($options);
$tableName = $this->_table->alias();
$isNew = $entity->isNew();
$keys = [];

if (isset($data[$tableName])) {
$data = $data[$tableName];
}

$errors = $this->_validate($data, $options, $entity->isNew());
if (!$isNew) {
$keys = $entity->extract((array)$this->_table->primaryKey());
}

$errors = $this->_validate($data + $keys, $options, $isNew);
$schema = $this->_table->schema();
$properties = [];
foreach ($data as $key => $value) {
Expand Down
6 changes: 5 additions & 1 deletion tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -1629,6 +1629,7 @@ public function testMergeWithValidation() {
];
$marshall = new Marshaller($this->articles);
$entity = new Entity([
'id' => 1,
'title' => 'Foo',
'body' => 'My Content',
'author_id' => 1
Expand All @@ -1639,14 +1640,17 @@ public function testMergeWithValidation() {

$this->articles->validator()
->requirePresence('thing', 'update')
->add('author_id', 'numeric', ['rule' => 'numeric']);
->requirePresence('id', 'update')
->add('author_id', 'numeric', ['rule' => 'numeric'])
->add('id', 'numeric', ['rule' => 'numeric', 'on' => 'update']);

$expected = clone $entity;
$result = $marshall->merge($expected, $data, []);

$this->assertSame($expected, $result);
$this->assertSame(1, $result->author_id);
$this->assertNotEmpty($result->errors('thing'));
$this->assertEmpty($result->errors('id'));

$this->articles->validator()->requirePresence('thing', 'create');
$result = $marshall->merge($entity, $data, []);
Expand Down

0 comments on commit f5cd576

Please sign in to comment.