Skip to content

Commit

Permalink
Moving the code around so that each association type can customize it
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 10, 2014
1 parent 8b506f3 commit bc4ee06
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
30 changes: 12 additions & 18 deletions src/ORM/Association.php
Expand Up @@ -463,24 +463,6 @@ public function transformRow($row, $nestKey, $joined) {
return $row;
}

/**
* Returns a modified row after appending a property for this association
* with the default empty value according to whether the association was
* joined or fetched externally.
*
* @param array $row
* @param boolean $joined Whether or not the row is a result of a direct join
* with this association
* @return array
*/
public function defaultRowValue($row, $joined) {
$sourceAlias = $this->source()->alias();
if (isset($row[$sourceAlias])) {
$row[$sourceAlias][$this->property()] = $joined ? null : [];
}
return $row;
}

/**
* Proxies the finding operation to the target table's find method
* and modifies the query accordingly based of this association
Expand Down Expand Up @@ -703,4 +685,16 @@ public abstract function isOwningSide(Table $side);
*/
public abstract function save(Entity $entity, $options = []);

/**
* Returns a modified row after appending a property for this association
* with the default empty value according to whether the association was
* joined or fetched externally.
*
* @param array $row
* @param boolean $joined Whether or not the row is a result of a direct join
* with this association
* @return array
*/
abstract public function defaultRowValue($row, $joined);

}
12 changes: 12 additions & 0 deletions src/ORM/Association/BelongsTo.php
Expand Up @@ -135,6 +135,18 @@ public function save(Entity $entity, $options = []) {
return $entity;
}

/**
* {@inheritdoc}
*
*/
public function defaultRowValue($row, $joined) {
$sourceAlias = $this->source()->alias();
if (isset($row[$sourceAlias])) {
$row[$sourceAlias][$this->property()] = null;
}
return $row;
}

/**
* Returns a single or multiple conditions to be appended to the generated join
* clause for getting the results on the target table.
Expand Down
12 changes: 12 additions & 0 deletions src/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -77,6 +77,18 @@ public function sort($sort = null) {
return $this->_sort;
}

/**
* {@inheritdoc}
*
*/
public function defaultRowValue($row, $joined) {
$sourceAlias = $this->source()->alias();
if (isset($row[$sourceAlias])) {
$row[$sourceAlias][$this->property()] = $joined ? null : [];
}
return $row;
}

/**
* Returns the default options to use for the eagerLoader
*
Expand Down
12 changes: 12 additions & 0 deletions src/ORM/Association/HasOne.php
Expand Up @@ -130,6 +130,18 @@ public function save(Entity $entity, $options = []) {
return $entity;
}

/**
* {@inheritdoc}
*
*/
public function defaultRowValue($row, $joined) {
$sourceAlias = $this->source()->alias();
if (isset($row[$sourceAlias])) {
$row[$sourceAlias][$this->property()] = null;
}
return $row;
}

/**
* {@inheritdoc}
*
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/AssociationTest.php
Expand Up @@ -52,7 +52,7 @@ public function setUp() {
'\Cake\ORM\Association',
[
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
'save', 'eagerLoader', 'type'
'save', 'eagerLoader', 'type', 'defaultRowValue'
],
['Foo', $config]
);
Expand Down

0 comments on commit bc4ee06

Please sign in to comment.