Skip to content

Commit

Permalink
Starting to move marshalling associated methods in Table to use $options
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 8, 2014
1 parent b1df652 commit c5ae11b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
18 changes: 8 additions & 10 deletions src/Datasource/RepositoryInterface.php
Expand Up @@ -146,11 +146,10 @@ public function delete(EntityInterface $entity, $options = []);
* is saved. Until the entity is saved, it will be a detached record.
*
* @param array $data The data to build an entity with.
* @param array $associations A whitelist of associations
* to hydrate. Defaults to all associations
* @param array $options A list of options for the object hydration.
* @return \Cake\Datasource\EntityInterface
*/
public function newEntity(array $data = [], $associations = null);
public function newEntity(array $data = [], array $options = []);

/**
* Create a list of entities + associated entities from an array.
Expand All @@ -165,11 +164,10 @@ public function newEntity(array $data = [], $associations = null);
* The hydrated entities can then be iterated and saved.
*
* @param array $data The data to build an entity with.
* @param array $associations A whitelist of associations
* to hydrate. Defaults to all associations
* @param array $options A list of options for the objects hydration.
* @return array An array of hydrated records.
*/
public function newEntities(array $data, $associations = null);
public function newEntities(array $data, array $associations = []);

/**
* Merges the passed `$data` into `$entity` respecting the accessible
Expand All @@ -185,10 +183,10 @@ public function newEntities(array $data, $associations = null);
* @param \Cake\Datasource\EntityInterface $entity the entity that will get the
* data merged in
* @param array $data key value list of fields to be merged into the entity
* @param array $associations The list of associations to be merged
* @param array $options A list of options for the object hydration.
* @return \Cake\Datasource\EntityInterface
*/
public function patchEntity(EntityInterface $entity, array $data, $associations = null);
public function patchEntity(EntityInterface $entity, array $data, array $options = []);

/**
* Merges each of the elements passed in `$data` into the entities
Expand All @@ -205,9 +203,9 @@ public function patchEntity(EntityInterface $entity, array $data, $associations
* @param array|\Traversable $entities the entities that will get the
* data merged in
* @param array $data list of arrays to be merged into the entities
* @param array $associations The list of associations to be merged
* @param array $options A list of options for the objects hydration.
* @return array
*/
public function patchEntities($entities, array $data, $associations = null);
public function patchEntities($entities, array $data, array $options = []);

}
9 changes: 7 additions & 2 deletions src/ORM/Marshaller.php
Expand Up @@ -59,9 +59,14 @@ public function __construct(Table $table) {
* @return array
*/
protected function _buildPropertyMap($options) {
if (empty($options['associated'])) {
return [];
}

$include = $options['associated'];
$map = [];
$options = $this->_normalizeAssociations($options);
foreach ($options as $key => $nested) {
$include = $this->_normalizeAssociations($include);
foreach ($include as $key => $nested) {
if (is_int($key) && is_scalar($nested)) {
$key = $nested;
$nested = [];
Expand Down
38 changes: 18 additions & 20 deletions src/ORM/Table.php
Expand Up @@ -1550,13 +1550,11 @@ public function __isset($property) {
* Override this method if you want a table object to use custom
* marshalling logic.
*
* @param bool $safe Whether or not this marshaller
* should be in safe mode.
* @return \Cake\ORM\Marshaller
* @see \Cake\ORM\Marshaller
*/
public function marshaller($safe = false) {
return new Marshaller($this, $safe);
public function marshaller() {
return new Marshaller($this);
}

/**
Expand All @@ -1576,22 +1574,22 @@ public function entityValidator() {
*
* By default all the associations on this table will be hydrated. You can
* limit which associations are built, or include deeper associations
* using the associations parameter:
* using the options parameter:
*
* {{{
* $articles = $this->Articles->newEntity(
* $this->request->data(),
* ['Tags', 'Comments.Users']
* ['associated' => ['Tags', 'Comments.Users']]
* );
* }}}
*
*/
public function newEntity(array $data = [], $associations = null) {
if ($associations === null) {
$associations = $this->_associations->keys();
public function newEntity(array $data = [], array $options = []) {
if (!isset($options['associated'])) {
$options['associated'] = $this->_associations->keys();
}
$marshaller = $this->marshaller();
return $marshaller->one($data, $associations);
return $marshaller->one($data, $options);
}

/**
Expand All @@ -1604,14 +1602,14 @@ public function newEntity(array $data = [], $associations = null) {
* {{{
* $articles = $this->Articles->newEntities(
* $this->request->data(),
* ['Tags', 'Comments.Users']
* ['associated' => ['Tags', 'Comments.Users']]
* );
* }}}
*
*/
public function newEntities(array $data, $associations = null) {
if ($associations === null) {
$associations = $this->_associations->keys();
public function newEntities(array $data, array $options = []) {
if (!isset($options['associated'])) {
$options['associated'] = $this->_associations->keys();
}
$marshaller = $this->marshaller();
return $marshaller->many($data, $associations);
Expand All @@ -1624,9 +1622,9 @@ public function newEntities(array $data, $associations = null) {
* `$data` array will appear, those that can be matched by primary key will get
* the data merged, but those that cannot, will be discarded.
*/
public function patchEntity(EntityInterface $entity, array $data, $associations = null) {
if ($associations === null) {
$associations = $this->_associations->keys();
public function patchEntity(EntityInterface $entity, array $data, array $options = []) {
if (!isset($options['associated'])) {
$options['associated'] = $this->_associations->keys();
}
$marshaller = $this->marshaller();
return $marshaller->merge($entity, $data, $associations);
Expand All @@ -1643,9 +1641,9 @@ public function patchEntity(EntityInterface $entity, array $data, $associations
* `$data` array will appear, those that can be matched by primary key will get
* the data merged, but those that cannot, will be discarded.
*/
public function patchEntities($entities, array $data, $associations = null) {
if ($associations === null) {
$associations = $this->_associations->keys();
public function patchEntities($entities, array $data, array $options = []) {
if (!isset($options['associated'])) {
$options['associated'] = $this->_associations->keys();
}
$marshaller = $this->marshaller();
return $marshaller->mergeMany($entities, $data, $associations);
Expand Down

0 comments on commit c5ae11b

Please sign in to comment.