Skip to content

Commit

Permalink
Add QueryTrait::getRepository().
Browse files Browse the repository at this point in the history
Deprecate using QueryInterface::repository() as getter.
  • Loading branch information
ADmad committed Apr 4, 2018
1 parent 44e2240 commit 0dcbda1
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Datasource/Paginator.php
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/Datasource/QueryInterface.php
Expand Up @@ -20,6 +20,7 @@
*
* @method $this andWhere($conditions, $types = [])
* @method $this select($fields = [], $overwrite = false)
* @method \Cake\Datasource\RepositoryInterface getRepository()
*/
interface QueryInterface
{
Expand Down
23 changes: 20 additions & 3 deletions src/Datasource/QueryTrait.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Behavior/TranslateBehavior.php
Expand Up @@ -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() ||
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/EagerLoader.php
Expand Up @@ -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;
}
Expand Down
28 changes: 16 additions & 12 deletions src/ORM/Query.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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', [
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}
Expand All @@ -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();
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/ORM/ResultSet.php
Expand Up @@ -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();
Expand Down
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase/Datasource/PaginatorTest.php
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -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
Expand Down

0 comments on commit 0dcbda1

Please sign in to comment.