From bc4ee063949f82338cd6ab30971425c5d7d270df Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 10 Apr 2014 10:16:34 +0200 Subject: [PATCH] Moving the code around so that each association type can customize it --- src/ORM/Association.php | 30 ++++++++----------- src/ORM/Association/BelongsTo.php | 12 ++++++++ .../Association/ExternalAssociationTrait.php | 12 ++++++++ src/ORM/Association/HasOne.php | 12 ++++++++ tests/TestCase/ORM/AssociationTest.php | 2 +- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/ORM/Association.php b/src/ORM/Association.php index 72bd537318c..d330ba6278b 100644 --- a/src/ORM/Association.php +++ b/src/ORM/Association.php @@ -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 @@ -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); + } diff --git a/src/ORM/Association/BelongsTo.php b/src/ORM/Association/BelongsTo.php index cd1375a61ff..e56c4156307 100644 --- a/src/ORM/Association/BelongsTo.php +++ b/src/ORM/Association/BelongsTo.php @@ -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. diff --git a/src/ORM/Association/ExternalAssociationTrait.php b/src/ORM/Association/ExternalAssociationTrait.php index 9442efedfae..d7fb178a89e 100644 --- a/src/ORM/Association/ExternalAssociationTrait.php +++ b/src/ORM/Association/ExternalAssociationTrait.php @@ -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 * diff --git a/src/ORM/Association/HasOne.php b/src/ORM/Association/HasOne.php index 1ad0ea147c5..26ced8d3997 100644 --- a/src/ORM/Association/HasOne.php +++ b/src/ORM/Association/HasOne.php @@ -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} * diff --git a/tests/TestCase/ORM/AssociationTest.php b/tests/TestCase/ORM/AssociationTest.php index b32588b4918..cd1c22e9ce8 100644 --- a/tests/TestCase/ORM/AssociationTest.php +++ b/tests/TestCase/ORM/AssociationTest.php @@ -52,7 +52,7 @@ public function setUp() { '\Cake\ORM\Association', [ '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide', - 'save', 'eagerLoader', 'type' + 'save', 'eagerLoader', 'type', 'defaultRowValue' ], ['Foo', $config] );