Skip to content

Commit

Permalink
Implemented dot notation for specifying associations in the marshaller
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 15, 2014
1 parent 33584b2 commit 5dd1ba2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
35 changes: 35 additions & 0 deletions src/ORM/Marshaller.php
Expand Up @@ -66,6 +66,7 @@ public function __construct(Table $table, $safe = false) {
*/
protected function _buildPropertyMap($include) {
$map = [];
$include = $this->_normalizeAssociations($include);
foreach ($include as $key => $nested) {
if (is_int($key) && is_scalar($nested)) {
$key = $nested;
Expand All @@ -83,6 +84,40 @@ protected function _buildPropertyMap($include) {
return $map;
}

/**
* Returns an array out of the original passed associations list where dot notation
* is transformed into nested arrays so that they can be parsed by other association
* marshallers.
*
* @param array $associations The array of included associations.
* @return array An array having dot notation trnasformed into nested arrays
*/
protected function _normalizeAssociations($associations) {
$result = [];
foreach ($associations as $table => $options) {
$pointer =& $result;

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

if (strpos($table, '.')) {
$path = explode('.', $table);
$table = array_pop($path);
foreach ($path as $t) {
$pointer += [$t => ['associated' => []]];
$pointer =& $pointer[$t]['associated'];
}
}

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

return $result;
}

/**
* Hydrate one entity and its associated data.
*
Expand Down
18 changes: 3 additions & 15 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -352,13 +352,7 @@ public function testOneBelongsToManyJoinDataAssociated() {
$articlesTags->belongsTo('Users');

$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, [
'Tags' => [
'associated' => [
'_joinData' => ['associated' => ['Users']]
]
]
]);
$result = $marshall->one($data, ['Tags._joinData.Users']);
$this->assertInstanceOf(
'Cake\ORM\Entity',
$result->tags[0]->_joinData->user,
Expand Down Expand Up @@ -392,7 +386,7 @@ public function testOneDeepAssociations() {
]
];
$marshall = new Marshaller($this->comments);
$result = $marshall->one($data, ['Articles' => ['associated' => ['Users']]]);
$result = $marshall->one($data, ['Articles.Users']);

$this->assertEquals(
$data['article']['title'],
Expand Down Expand Up @@ -870,13 +864,7 @@ public function testMergeJoinDataAssociations() {
$articlesTags = TableRegistry::get('ArticlesTags');
$articlesTags->belongsTo('Users');

$options = [
'Tags' => [
'associated' => [
'_joinData' => ['associated' => ['Users']]
]
]
];
$options = ['Tags._joinData.Users'];
$marshall = new Marshaller($this->articles);
$entity = $marshall->one($data, $options);
$entity->accessible('*', true);
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -151,7 +151,7 @@ public function testCreateJointData() {
]
]
];
$entity = $articles->patchEntity($entity, $data, ['Highlights' => ['associated' => ['_joinData']]]);
$entity = $articles->patchEntity($entity, $data, ['Highlights._joinData']);
$articles->save($entity);
$entity = $articles->get(2, ['contain' => ['Highlights']]);
$this->assertEquals(4, $entity->highlights[0]->_joinData->tag_id);
Expand Down Expand Up @@ -214,7 +214,7 @@ public function testBelongsToManyDeepSave() {
]
];
$entity = $articles->patchEntity($entity, $data, [
'Highlights' => ['associated' => ['_joinData' => ['associated' => ['Authors']], 'Authors']]
'Highlights._joinData.Authors', 'Highlights.Authors'
]);
$articles->save($entity, [
'associated' => [
Expand Down

0 comments on commit 5dd1ba2

Please sign in to comment.