Skip to content

Commit

Permalink
Starting to implement dot notation for association options in Table::…
Browse files Browse the repository at this point in the history
…save()
  • Loading branch information
lorenzo committed Jun 15, 2014
1 parent 5dd1ba2 commit a751d37
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
35 changes: 30 additions & 5 deletions src/ORM/Associations.php
Expand Up @@ -263,12 +263,37 @@ public function normalizeKeys($keys) {
}

$result = [];
foreach ($keys as $key => $value) {
if (is_int($key)) {
$key = $value;
$value = [];
foreach ($keys as $table => $options) {
$pointer =& $result;

if (is_int($table)) {
$table = $options;
$options = [];
}

if (!strpos($table, '.')) {
$result[$table] = $options;
continue;
}

$path = explode('.', $table);
$table = array_pop($path);
$first = array_shift($path);

if (isset($pointer[$first])) {
$pointer =& $pointer[$first];
$pointer += ['associated' => []];
}
$result[$key] = $value;

foreach ($path as $t) {
$pointer += ['associated' => []];
$pointer['associated'] += [$t => []];
$pointer['associated'][$t] += ['associated' => []];
$pointer =& $pointer['associated'][$t];
}

$pointer['associated'] += [$table => []];
$pointer['associated'][$table] = $options + $pointer['associated'][$table];
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -218,7 +218,7 @@ public function testBelongsToManyDeepSave() {
]);
$articles->save($entity, [
'associated' => [
'Highlights' => ['associated' => ['_joinData' => ['associated' => ['Authors']], 'Authors']]
'Highlights._joinData.Authors', 'Highlights.Authors'
]
]);
$entity = $articles->get(2, ['contain' => ['SpecialTags.Authors', 'Highlights.Authors']]);
Expand Down
14 changes: 6 additions & 8 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -2919,14 +2919,12 @@ public function testSaveDeepAssociationOptions() {
$this->assertSame($entity, $articles->save($entity, [
'associated' => [
'authors' => [
'validate' => 'special',
'associated' => [
'supervisors' => [
'atomic' => false,
'validate' => false,
'associated' => false
]
]
'validate' => 'special'
],
'authors.supervisors' => [
'atomic' => false,
'validate' => false,
'associated' => false
]
]
]));
Expand Down

0 comments on commit a751d37

Please sign in to comment.