Skip to content
Permalink
Browse files

Add QueryTrait::getRepository().

Deprecate using QueryInterface::repository() as getter.
  • Loading branch information...
ADmad committed Apr 1, 2018
1 parent 44e2240 commit 0dcbda1da53f7f2775cb377cdc3eb0aad6a1ea8d
@@ -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();
@@ -20,6 +20,7 @@
*
* @method $this andWhere($conditions, $types = [])
* @method $this select($fields = [], $overwrite = false)
* @method \Cake\Datasource\RepositoryInterface getRepository()
*/
interface QueryInterface
{
@@ -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()
));
}
@@ -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() ||
@@ -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;
}
@@ -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);
@@ -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();
@@ -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;
}
@@ -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;
}
@@ -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')
@@ -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,
@@ -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

0 comments on commit 0dcbda1

Please sign in to comment.
You can’t perform that action at this time.