Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Normalizing associated tables array before passing it to the event

  • Loading branch information...
commit e3c730d097da38434fa1d818d3d53b619bdfa61c 1 parent 9499b17
@lorenzo lorenzo authored
View
29 src/ORM/Associations.php
@@ -231,4 +231,33 @@ public function cascadeDelete(Entity $entity, $options) {
}
}
+/**
+ * Returns an associative array of association names out a mixed
+ * array. If true is passed, then it returns all association names
+ * in this collection.
+ *
+ * @param boolean|array $keys the list of association names to normalize
+ * @return array
+ */
+ public function normalizeKeys($keys) {
+ if ($keys === true) {
+ $keys = $this->keys();
+ }
+
+ if (empty($keys)) {
+ return [];
+ }
+
+ $result = [];
+ foreach ($keys as $key => $value) {
+ if (is_int($key)) {
+ $key = $value;
+ $value = [];
+ }
+ $result[$key] = $value;
+ }
+
+ return $result;
+ }
+
}
View
7 src/ORM/Table.php
@@ -1170,17 +1170,14 @@ protected function _processSave($entity, $options) {
$entity->isNew(true);
}
- if ($options['associated'] === true) {
- $options['associated'] = $this->_associated->keys();
- }
- $associated = array_filter((array)$options['associated']);
+ $associated = $options['associated'];
$options['associated'] = [];
if (!$this->validate($entity, $options)) {
return false;
}
- $options['associated'] = $associated;
+ $options['associated'] = $this->_associated->normalizeKeys($associated);
$event = new Event('Model.beforeSave', $this, compact('entity', 'options'));
$this->getEventManager()->dispatch($event);
View
5 tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
@@ -597,6 +597,11 @@ public function testSaveMultipleTranslations() {
$translations['eng']->set('body', 'Another body');
$article->set('_translations', $translations);
$table->save($article);
+
+ $article = $results = $table->find('translations')->first();
+ $translations = $article->get('_translations');
+ $this->assertEquals('Another title', $translations['deu']->get('title'));
+ $this->assertEquals('Another body', $translations['eng']->get('body'));
}
}
View
20 tests/TestCase/ORM/AssociationsTest.php
@@ -303,4 +303,24 @@ public function testErrorOnUnknownAlias() {
);
}
+/**
+ * Tests the normalizeKeys method
+ *
+ * @return void
+ */
+ public function testNormalizeKeys() {
+ $this->assertSame([], $this->associations->normalizeKeys([]));
+ $this->assertSame([], $this->associations->normalizeKeys(false));
+
+ $assocs = ['a', 'b', 'd' => ['something']];
+ $expected = ['a' => [], 'b' => [], 'd' => ['something']];
+ $this->assertSame($expected, $this->associations->normalizeKeys($assocs));
+
+ $belongsTo = new BelongsTo([]);
+ $this->associations->add('users', $belongsTo);
+ $this->associations->add('categories', $belongsTo);
+ $expected = ['users' => [], 'categories' => []];
+ $this->assertSame($expected, $this->associations->normalizeKeys(true));
+ }
+
}

0 comments on commit e3c730d

Please sign in to comment.
Something went wrong with that request. Please try again.