Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Documenting new methods and fixing a couple CS issues

  • Loading branch information...
commit 1c026ff0bad0886710a57f6fee44c8890eb426d5 1 parent 2164100
@lorenzo lorenzo authored
Showing with 58 additions and 4 deletions.
  1. +56 −0 src/ORM/Marshaller.php
  2. +2 −4 tests/TestCase/ORM/MarshallerTest.php
View
56 src/ORM/Marshaller.php
@@ -216,6 +216,22 @@ protected function _loadBelongsToMany($assoc, $ids) {
return $assoc->find()->where($filter)->toArray();
}
+/**
+ * Merges `$data` into `$entity` and recursively does the same for each one of
+ * the association names passed in `$include`. When merging association, if an
+ * entity is not present in the parent entity for such association, a new one
+ * will be created.
+ *
+ * When merging HasMany or BelongsToMany associations, all the entities in the
+ * `$data` array will appear, those that can be matched by primary key will get
+ * the data merged, but those that cannot, will be discarded.
+ *
+ * @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 $include The list of associations to be merged
+ * @return \Cake\Datasource\EntityInterface
+ */
public function merge(EntityInterface $entity, array $data, $include = []) {
$propertyMap = $this->_buildPropertyMap($include);
$tableName = $this->_table->alias();
@@ -241,6 +257,27 @@ public function merge(EntityInterface $entity, array $data, $include = []) {
return $entity;
}
+/**
+ * Merges each of the elements from `$data` into each of the entities in `$entities
+ * and recursively does the same for each one of the association names passed in
+ * `$include`. When merging association, if an entity is not present in the parent
+ * entity for such association, a new one will be created.
+ *
+ * Records in `$data` are matched against the entities by using the primary key
+ * column. Those entries in `$entities` that cannot be matched to any record in
+ * `$data` will be discarded. Records in `$data` that could not be matched will
+ * be marshaled as a new entity.
+ *
+ * When merging HasMany or BelongsToMany associations, all the entities in the
+ * `$data` array will appear, those that can be matched by primary key will get
+ * the data merged, but those that cannot, will be discarded.
+ *
+ * @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 $include The list of associations to be merged
+ * @return array
+ */
public function mergeMany($entities, array $data, $include = []) {
$primary = (array)$this->_table->primaryKey();
$indexed = (new Collection($data))->groupBy($primary[0])->toArray();
@@ -271,6 +308,15 @@ public function mergeMany($entities, array $data, $include = []) {
return $output;
}
+/**
+ * Creates a new sub-marshaller and merges the associated data.
+ *
+ * @param \Cake\Datasource\EntityInterface $original
+ * @param \Cake\ORM\Association $assoc
+ * @param array $value The data to hydrate
+ * @param array $include The associations to include.
+ * @return mixed
+ */
protected function _mergeAssociation($original, $assoc, $value, $include) {
if (!$original) {
return $this->_marshalAssociation($assoc, $value, $include);
@@ -287,6 +333,16 @@ protected function _mergeAssociation($original, $assoc, $value, $include) {
return $marshaller->mergeMany($original, $value, (array)$include);
}
+/**
+ * Creates a new sub-marshaller and merges the associated data for a BelongstoMany
+ * association.
+ *
+ * @param \Cake\Datasource\EntityInterface $original
+ * @param \Cake\ORM\Association $assoc
+ * @param array $value The data to hydrate
+ * @param array $include The associations to include.
+ * @return mixed
+ */
protected function _mergeBelongsToMany($original, $assoc, $data, $include) {
if (isset($data['_ids']) && is_array($data['_ids'])) {
return $this->_loadBelongsToMany($assoc, $data['_ids']);
View
6 tests/TestCase/ORM/MarshallerTest.php
@@ -592,7 +592,7 @@ public function testMergeWithSingleAssociation() {
]
];
$marshall = new Marshaller($this->articles);
- $result = $marshall->merge($entity, $data, ['Users']);
+ $marshall->merge($entity, $data, ['Users']);
$this->assertEquals('My Content', $entity->body);
$this->assertSame($user, $entity->user);
$this->assertEquals('mark', $entity->user->username);
@@ -619,7 +619,7 @@ public function testMergeCreateAssociation() {
]
];
$marshall = new Marshaller($this->articles);
- $result = $marshall->merge($entity, $data, ['Users']);
+ $marshall->merge($entity, $data, ['Users']);
$this->assertEquals('My Content', $entity->body);
$this->assertInstanceOf('Cake\ORM\Entity', $entity->user);
$this->assertEquals('mark', $entity->user->username);
@@ -902,8 +902,6 @@ public function testMergeManyWithAppend() {
$this->assertNotSame($entities[0], $result[0]);
$this->assertSame($entities[1], $result[0]);
$this->assertEquals('Changed 2', $result[0]->comment);
-
}
-
}
Please sign in to comment.
Something went wrong with that request. Please try again.