Browse Source

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

Jose Lorenzo Rodriguez 11 years ago
parent
commit
c5ae11b35f
3 changed files 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

+ 8 - 10
src/Datasource/RepositoryInterface.php

@@ -146,11 +146,10 @@ interface RepositoryInterface {
  * 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 @@ interface RepositoryInterface {
  * 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 @@ interface RepositoryInterface {
  * @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 @@ interface RepositoryInterface {
  * @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 = []);
 
 }

+ 7 - 2
src/ORM/Marshaller.php

@@ -59,9 +59,14 @@ class Marshaller {
  * @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 = [];

+ 18 - 20
src/ORM/Table.php

@@ -1550,13 +1550,11 @@ class Table implements RepositoryInterface, EventListener {
  * 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 @@ class Table implements RepositoryInterface, EventListener {
  *
  * 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 @@ class Table implements RepositoryInterface, EventListener {
  * {{{
  * $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 @@ class Table implements RepositoryInterface, EventListener {
  * `$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 @@ class Table implements RepositoryInterface, EventListener {
  * `$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);