Navigation Menu

Skip to content

Commit

Permalink
Make use of modifiable $options from beforeMarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Jan 23, 2015
1 parent beed273 commit b9db56d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
27 changes: 13 additions & 14 deletions src/ORM/Marshaller.php
Expand Up @@ -100,7 +100,8 @@ protected function _buildPropertyMap($options)
*/
public function one(array $data, array $options = [])
{
$options += ['validate' => true];
list($data, $options) = $this->_prepareDataAndOptions($data, $options);

$propertyMap = $this->_buildPropertyMap($options);

$schema = $this->_table->schema();
Expand All @@ -114,8 +115,6 @@ public function one(array $data, array $options = [])
}
}

$data = $this->_prepareData($data, $options);

$errors = $this->_validate($data, $options, true);
$primaryKey = $schema->primaryKey();
$properties = [];
Expand Down Expand Up @@ -183,25 +182,26 @@ protected function _validate($data, $options, $isNew)
}

/**
* Returns data prepared to validate and marshall.
* Returns data and options prepared to validate and marshall.
*
* @param array $data The data to prepare.
* @param array $options The options passed to this marshaller.
* @return array Prepared data.
* @return array An array containing prepared data and options.
*/
protected function _prepareData($data, $options)
protected function _prepareDataAndOptions($data, $options)
{
$tableName = $this->_table->alias();
$options += ['validate' => true];

$tableName = $this->_table->alias();
if (isset($data[$tableName])) {
$data = $data[$tableName];
}

$dataObject = new \ArrayObject($data);
$data = new \ArrayObject($data);
$options = new \ArrayObject($options);
$this->_table->dispatchEvent('Model.beforeMarshal', compact('dataObject', 'options'));
$this->_table->dispatchEvent('Model.beforeMarshal', compact('data', 'options'));

return (array)$dataObject;
return [(array)$data, (array)$options];
}

/**
Expand Down Expand Up @@ -343,7 +343,8 @@ protected function _loadBelongsToMany($assoc, $ids)
*/
public function merge(EntityInterface $entity, array $data, array $options = [])
{
$options += ['validate' => true];
list($data, $options) = $this->_prepareDataAndOptions($data, $options);

$propertyMap = $this->_buildPropertyMap($options);
$isNew = $entity->isNew();
$keys = [];
Expand All @@ -352,8 +353,6 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
$keys = $entity->extract((array)$this->_table->primaryKey());
}

$data = $this->_prepareData($data, $options);

$errors = $this->_validate($data + $keys, $options, $isNew);
$schema = $this->_table->schema();
$properties = [];
Expand All @@ -373,7 +372,7 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
$value = $converter->marshal($value);
$isObject = is_object($value);
if ((!$isObject && $original === $value) ||
($isObject && $original == $value)
($isObject && $original == $value)
) {
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -1764,14 +1764,14 @@ public function testBeforeMarshalEvent()

$marshall = new Marshaller($this->articles);

$this->articles->eventManager()->attach(function ($e, $data) {
$this->articles->eventManager()->attach(function ($e, $data, $options) {
$data['title'] = 'Modified title';
$data['user']['username'] = 'robert';

$options['associated'] = ['Users'];
}, 'Model.beforeMarshal');

$entity = $marshall->one($data, [
'associated' => ['Users']
]);
$entity = $marshall->one($data);

$this->assertEquals('Modified title', $entity->title);
$this->assertEquals('My content', $entity->body);
Expand Down

0 comments on commit b9db56d

Please sign in to comment.