diff --git a/src/Datasource/Paginator.php b/src/Datasource/Paginator.php index 198276311a7..ac3332a88ac 100644 --- a/src/Datasource/Paginator.php +++ b/src/Datasource/Paginator.php @@ -163,7 +163,7 @@ public function paginate($object, array $params = [], array $settings = []) $query = null; if ($object instanceof QueryInterface) { $query = $object; - $object = $query->repository(); + $object = $query->getRepository(); } $alias = $object->getAlias(); diff --git a/src/Datasource/QueryInterface.php b/src/Datasource/QueryInterface.php index 48bf0cc77e0..793258e5df2 100644 --- a/src/Datasource/QueryInterface.php +++ b/src/Datasource/QueryInterface.php @@ -20,6 +20,7 @@ * * @method $this andWhere($conditions, $types = []) * @method $this select($fields = [], $overwrite = false) + * @method \Cake\Datasource\RepositoryInterface getRepository() */ interface QueryInterface { diff --git a/src/Datasource/QueryTrait.php b/src/Datasource/QueryTrait.php index 55df60a26c3..c0abf4410fe 100644 --- a/src/Datasource/QueryTrait.php +++ b/src/Datasource/QueryTrait.php @@ -94,13 +94,30 @@ trait QueryTrait public function repository(RepositoryInterface $table = null) { if ($table === null) { - return $this->_repository; + deprecationWarning( + 'Using Query::repository() as getter is deprecated. ' . + 'Use getRepository() instead.' + ); + + return $this->getRepository(); } + $this->_repository = $table; return $this; } + /** + * Returns the default table object that will be used by this query, + * that is, the table that will appear in the from clause. + * + * @return \Cake\Datasource\RepositoryInterface|\Cake\ORM\Table + */ + public function getRepository() + { + return $this->_repository; + } + /** * Set the result set for a query. * @@ -236,7 +253,7 @@ public function aliasField($field, $alias = null) } if (!$alias) { - $alias = $this->repository()->getAlias(); + $alias = $this->getRepository()->getAlias(); } $key = sprintf('%s__%s', $alias, $field); @@ -467,7 +484,7 @@ public function firstOrFail() if (!$entity) { throw new RecordNotFoundException(sprintf( 'Record not found in table "%s"', - $this->repository()->getTable() + $this->getRepository()->getTable() )); } diff --git a/src/ORM/Behavior/TranslateBehavior.php b/src/ORM/Behavior/TranslateBehavior.php index 840aaf2ddb5..173db6d335c 100644 --- a/src/ORM/Behavior/TranslateBehavior.php +++ b/src/ORM/Behavior/TranslateBehavior.php @@ -218,7 +218,7 @@ public function beforeFind(Event $event, Query $query, ArrayObject $options) $conditions = function ($field, $locale, $query, $select) { return function ($q) use ($field, $locale, $query, $select) { /* @var \Cake\Datasource\QueryInterface $q */ - $q->where([$q->repository()->aliasField('locale') => $locale]); + $q->where([$q->getRepository()->aliasField('locale') => $locale]); /* @var \Cake\ORM\Query $query */ if ($query->isAutoFieldsEnabled() || diff --git a/src/ORM/EagerLoader.php b/src/ORM/EagerLoader.php index 449b9b02e4b..749f4366563 100644 --- a/src/ORM/EagerLoader.php +++ b/src/ORM/EagerLoader.php @@ -671,7 +671,7 @@ protected function _resolveJoins($associations, $matching = []) */ public function loadExternal($query, $statement) { - $external = $this->externalAssociations($query->repository()); + $external = $this->externalAssociations($query->getRepository()); if (empty($external)) { return $statement; } diff --git a/src/ORM/Query.php b/src/ORM/Query.php index 5a08032385f..210201bf662 100644 --- a/src/ORM/Query.php +++ b/src/ORM/Query.php @@ -444,7 +444,11 @@ public function contain($associations = null, $override = false) if ($associations) { $loader->contain($associations, $queryBuilder); } - $this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $loader->getContain()); + $this->_addAssociationsToTypeMap( + $this->getRepository(), + $this->getTypeMap(), + $loader->getContain() + ); return $this; } @@ -551,7 +555,7 @@ protected function _addAssociationsToTypeMap($table, $typeMap, $associations) public function matching($assoc, callable $builder = null) { $result = $this->getEagerLoader()->setMatching($assoc, $builder)->getMatching(); - $this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $result); + $this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result); $this->_dirty(); return $this; @@ -628,7 +632,7 @@ public function leftJoinWith($assoc, callable $builder = null) 'fields' => false ]) ->getMatching(); - $this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $result); + $this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result); $this->_dirty(); return $this; @@ -677,7 +681,7 @@ public function innerJoinWith($assoc, callable $builder = null) 'fields' => false ]) ->getMatching(); - $this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $result); + $this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result); $this->_dirty(); return $this; @@ -742,7 +746,7 @@ public function notMatching($assoc, callable $builder = null) 'negateMatch' => true ]) ->getMatching(); - $this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $result); + $this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result); $this->_dirty(); return $this; @@ -1051,7 +1055,7 @@ public function all() public function triggerBeforeFind() { if (!$this->_beforeFindFired && $this->_type === 'select') { - $table = $this->repository(); + $table = $this->getRepository(); $this->_beforeFindFired = true; /* @var \Cake\Event\EventDispatcherInterface $table */ $table->dispatchEvent('Model.beforeFind', [ @@ -1134,11 +1138,11 @@ protected function _addDefaultFields() if (!count($select) || $this->_autoFields === true) { $this->_hasFields = false; - $this->select($this->repository()->getSchema()->columns()); + $this->select($this->getRepository()->getSchema()->columns()); $select = $this->clause('select'); } - $aliased = $this->aliasFields($select, $this->repository()->getAlias()); + $aliased = $this->aliasFields($select, $this->getRepository()->getAlias()); $this->select($aliased, true); } @@ -1175,7 +1179,7 @@ protected function _addDefaultSelectTypes() */ public function find($finder, array $options = []) { - return $this->repository()->callFinder($finder, $this, $options); + return $this->getRepository()->callFinder($finder, $this, $options); } /** @@ -1202,7 +1206,7 @@ protected function _dirty() */ public function update($table = null) { - $table = $table ?: $this->repository()->getTable(); + $table = $table ?: $this->getRepository()->getTable(); return parent::update($table); } @@ -1218,7 +1222,7 @@ public function update($table = null) */ public function delete($table = null) { - $repo = $this->repository(); + $repo = $this->getRepository(); $this->from([$repo->getAlias() => $repo->getTable()]); return parent::delete(); @@ -1239,7 +1243,7 @@ public function delete($table = null) */ public function insert(array $columns, array $types = []) { - $table = $this->repository()->getTable(); + $table = $this->getRepository()->getTable(); $this->into($table); return parent::insert($columns, $types); diff --git a/src/ORM/ResultSet.php b/src/ORM/ResultSet.php index 3ad2cb1db31..725d583c715 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -175,10 +175,10 @@ class ResultSet implements ResultSetInterface */ public function __construct($query, $statement) { - $repository = $query->repository(); + $repository = $query->getRepository(); $this->_statement = $statement; $this->_driver = $query->getConnection()->getDriver(); - $this->_defaultTable = $query->repository(); + $this->_defaultTable = $query->getRepository(); $this->_calculateAssociationMap($query); $this->_hydrate = $query->isHydrationEnabled(); $this->_entityClass = $repository->getEntityClass(); diff --git a/tests/TestCase/Controller/Component/PaginatorComponentTest.php b/tests/TestCase/Controller/Component/PaginatorComponentTest.php index 093c456d469..fb2bac228b0 100644 --- a/tests/TestCase/Controller/Component/PaginatorComponentTest.php +++ b/tests/TestCase/Controller/Component/PaginatorComponentTest.php @@ -1405,7 +1405,9 @@ protected function _getMockFindQuery($table = null) ->method('count') ->will($this->returnValue(2)); - $query->repository($table); + if ($table) { + $query->repository($table); + } return $query; } diff --git a/tests/TestCase/Datasource/PaginatorTest.php b/tests/TestCase/Datasource/PaginatorTest.php index 0e219e8fcd5..629363632ce 100644 --- a/tests/TestCase/Datasource/PaginatorTest.php +++ b/tests/TestCase/Datasource/PaginatorTest.php @@ -1380,7 +1380,9 @@ protected function _getMockFindQuery($table = null) ->method('count') ->will($this->returnValue(2)); - $query->repository($table); + if ($table) { + $query->repository($table); + } return $query; } diff --git a/tests/TestCase/ORM/Association/HasManyTest.php b/tests/TestCase/ORM/Association/HasManyTest.php index 3c4619438dd..0add855d071 100644 --- a/tests/TestCase/ORM/Association/HasManyTest.php +++ b/tests/TestCase/ORM/Association/HasManyTest.php @@ -440,7 +440,7 @@ public function testEagerLoaderMultipleKeys() $keys = [[1, 10], [2, 20], [3, 30], [4, 40]]; $query = $this->getMockBuilder('Cake\ORM\Query') ->setMethods(['all', 'andWhere']) - ->setConstructorArgs([null, null]) + ->disableOriginalConstructor() ->getMock(); $this->article->method('find') ->with('all') diff --git a/tests/TestCase/ORM/Association/HasOneTest.php b/tests/TestCase/ORM/Association/HasOneTest.php index b93164dc915..a10d1b14d4a 100644 --- a/tests/TestCase/ORM/Association/HasOneTest.php +++ b/tests/TestCase/ORM/Association/HasOneTest.php @@ -186,7 +186,7 @@ public function testAttachToMultiPrimaryKey() $query = $this->getMockBuilder('\Cake\ORM\Query') ->setMethods(['join', 'select']) - ->setConstructorArgs([null, null]) + ->disableOriginalConstructor() ->getMock(); $field1 = new IdentifierExpression('Profiles.user_id'); $field2 = new IdentifierExpression('Profiles.user_site_id'); @@ -216,7 +216,7 @@ public function testAttachToMultiPrimaryKeyMismatch() $this->expectExceptionMessage('Cannot match provided foreignKey for "Profiles", got "(user_id)" but expected foreign key for "(id, site_id)"'); $query = $this->getMockBuilder('\Cake\ORM\Query') ->setMethods(['join', 'select']) - ->setConstructorArgs([null, null]) + ->disableOriginalConstructor() ->getMock(); $config = [ 'sourceTable' => $this->user, diff --git a/tests/TestCase/ORM/QueryTest.php b/tests/TestCase/ORM/QueryTest.php index 3f615ab8159..5dc9e0b606d 100644 --- a/tests/TestCase/ORM/QueryTest.php +++ b/tests/TestCase/ORM/QueryTest.php @@ -138,6 +138,19 @@ public function strategiesProviderBelongsToMany() return [['subquery'], ['select']]; } + /** + * Test getRepository() method. + * + * @return void + */ + public function testGetRepository() + { + $query = new Query($this->connection, $this->table); + + $result = $query->getRepository(); + $this->assertSame($this->table, $result); + } + /** * Tests that results are grouped correctly when using contain() * and results are not hydrated