Skip to content
Permalink
Browse files

Starting to move marshalling associated methods in Table to use $options

  • Loading branch information...
lorenzo committed Jul 8, 2014
1 parent b1df652 commit c5ae11b35fd019cf2a8d7b65ec4b1b1053eefed2
Showing with 33 additions and 32 deletions.
  1. +8 −10 src/Datasource/RepositoryInterface.php
  2. +7 −2 src/ORM/Marshaller.php
  3. +18 −20 src/ORM/Table.php
@@ -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.
@@ -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
@@ -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
@@ -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 = []);
}
@@ -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 = [];
@@ -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);
}
/**
@@ -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);
}
/**
@@ -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);
@@ -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);
@@ -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);

0 comments on commit c5ae11b

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