Skip to content

Commit

Permalink
Adding support both hasMany and BelongsToMany + support marshall
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetansnl authored and markstory committed Sep 3, 2015
1 parent 7cbf290 commit 253b382
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/ORM/Marshaller.php
Expand Up @@ -223,20 +223,12 @@ protected function _marshalAssociation($assoc, $value, $options)
if (in_array($assoc->type(), $types)) {
return $marshaller->one($value, (array)$options);
}
if ($assoc->type() === Association::ONE_TO_MANY || $assoc->type() === Association::MANY_TO_MANY) {
$hasIds = array_key_exists('_ids', $data);
$idsOption = array_key_exists('ids', $options) && $options['ids'];

if ($hasIds && is_array($data['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $data['_ids']);
}
if ($hasIds || $idsOption) {
return [];
}
}
if ($assoc->type() === Association::MANY_TO_MANY) {
return $marshaller->_belongsToMany($assoc, $value, (array)$options);
}
if ($assoc->type() === Association::ONE_TO_MANY && array_key_exists('_ids', $value) && is_array($value['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $value['_ids']);
}
return $marshaller->many($value, (array)$options);
}

Expand Down Expand Up @@ -280,8 +272,15 @@ public function many(array $data, array $options = [])
*/
protected function _belongsToMany(Association $assoc, array $data, $options = [])
{
// Accept _ids = [1, 2]
$associated = isset($options['associated']) ? $options['associated'] : [];

$hasIds = array_key_exists('_ids', $data);
if ($hasIds && is_array($data['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $data['_ids']);
}
if ($hasIds) {
return [];
}
$data = array_values($data);

$target = $assoc->target();
Expand Down Expand Up @@ -622,15 +621,14 @@ protected function _mergeAssociation($original, $assoc, $value, $options)
*/
protected function _mergeBelongsToMany($original, $assoc, $value, $options)
{
$hasIds = array_key_exists('_ids', $value);
$associated = isset($options['associated']) ? $options['associated'] : [];
$_ids = array_key_exists('_ids', $options) && $options['_ids'];

$hasIds = array_key_exists('_ids', $data);
$idsOption = array_key_exists('ids', $options) && $options['ids'];

if ($hasIds && is_array($data['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $data['_ids']);
if ($hasIds && is_array($value['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $value['_ids']);
}
if ($hasIds || $idsOption) {
if ($hasIds || $_ids) {
return [];
}

Expand Down

0 comments on commit 253b382

Please sign in to comment.