From 535f99f733b77636b8987783a73c2c211e0fc56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Tue, 12 Sep 2017 09:39:39 +0200 Subject: [PATCH] Pass the locator to AssociationCollection. --- src/ORM/AssociationCollection.php | 2 ++ src/ORM/Locator/TableLocator.php | 6 ++++++ tests/TestCase/ORM/Locator/TableLocatorTest.php | 2 ++ 3 files changed, 10 insertions(+) diff --git a/src/ORM/AssociationCollection.php b/src/ORM/AssociationCollection.php index ee6618fe182..3d3bd38db53 100644 --- a/src/ORM/AssociationCollection.php +++ b/src/ORM/AssociationCollection.php @@ -16,6 +16,7 @@ use ArrayIterator; use Cake\Datasource\EntityInterface; +use Cake\ORM\Locator\LocatorAwareTrait; use InvalidArgumentException; use IteratorAggregate; @@ -29,6 +30,7 @@ class AssociationCollection implements IteratorAggregate { use AssociationsNormalizerTrait; + use LocatorAwareTrait; /** * Stored associations diff --git a/src/ORM/Locator/TableLocator.php b/src/ORM/Locator/TableLocator.php index 36ab5cdaaa7..cb766a44225 100644 --- a/src/ORM/Locator/TableLocator.php +++ b/src/ORM/Locator/TableLocator.php @@ -16,6 +16,7 @@ use Cake\Core\App; use Cake\Datasource\ConnectionManager; +use Cake\ORM\AssociationCollection; use Cake\ORM\Table; use Cake\Utility\Inflector; use RuntimeException; @@ -211,6 +212,11 @@ public function get($alias, array $options = []) } $options['connection'] = ConnectionManager::get($connectionName); } + if (empty($options['associations'])) { + $associations = new AssociationCollection(); + $associations->setTableLocator($this); + $options['associations'] = $associations; + } $options['registryAlias'] = $alias; $this->_instances[$alias] = $this->_create($options); diff --git a/tests/TestCase/ORM/Locator/TableLocatorTest.php b/tests/TestCase/ORM/Locator/TableLocatorTest.php index f8f6bdef831..1fe3efc31b2 100644 --- a/tests/TestCase/ORM/Locator/TableLocatorTest.php +++ b/tests/TestCase/ORM/Locator/TableLocatorTest.php @@ -164,6 +164,8 @@ public function testGet() $result2 = $this->_locator->get('Articles'); $this->assertSame($result, $result2); $this->assertEquals('my_articles', $result->table()); + + $this->assertSame($this->_locator, $result->associations()->getTableLocator()); } /**