Skip to content

Commit 0dcbda1

Browse files
committed
Add QueryTrait::getRepository().
Deprecate using QueryInterface::repository() as getter.
1 parent 44e2240 commit 0dcbda1

File tree

12 files changed

+64
-25
lines changed

12 files changed

+64
-25
lines changed

src/Datasource/Paginator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function paginate($object, array $params = [], array $settings = [])
163163
$query = null;
164164
if ($object instanceof QueryInterface) {
165165
$query = $object;
166-
$object = $query->repository();
166+
$object = $query->getRepository();
167167
}
168168

169169
$alias = $object->getAlias();

src/Datasource/QueryInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*
2121
* @method $this andWhere($conditions, $types = [])
2222
* @method $this select($fields = [], $overwrite = false)
23+
* @method \Cake\Datasource\RepositoryInterface getRepository()
2324
*/
2425
interface QueryInterface
2526
{

src/Datasource/QueryTrait.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,30 @@ trait QueryTrait
9494
public function repository(RepositoryInterface $table = null)
9595
{
9696
if ($table === null) {
97-
return $this->_repository;
97+
deprecationWarning(
98+
'Using Query::repository() as getter is deprecated. ' .
99+
'Use getRepository() instead.'
100+
);
101+
102+
return $this->getRepository();
98103
}
104+
99105
$this->_repository = $table;
100106

101107
return $this;
102108
}
103109

110+
/**
111+
* Returns the default table object that will be used by this query,
112+
* that is, the table that will appear in the from clause.
113+
*
114+
* @return \Cake\Datasource\RepositoryInterface|\Cake\ORM\Table
115+
*/
116+
public function getRepository()
117+
{
118+
return $this->_repository;
119+
}
120+
104121
/**
105122
* Set the result set for a query.
106123
*
@@ -236,7 +253,7 @@ public function aliasField($field, $alias = null)
236253
}
237254

238255
if (!$alias) {
239-
$alias = $this->repository()->getAlias();
256+
$alias = $this->getRepository()->getAlias();
240257
}
241258

242259
$key = sprintf('%s__%s', $alias, $field);
@@ -467,7 +484,7 @@ public function firstOrFail()
467484
if (!$entity) {
468485
throw new RecordNotFoundException(sprintf(
469486
'Record not found in table "%s"',
470-
$this->repository()->getTable()
487+
$this->getRepository()->getTable()
471488
));
472489
}
473490

src/ORM/Behavior/TranslateBehavior.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function beforeFind(Event $event, Query $query, ArrayObject $options)
218218
$conditions = function ($field, $locale, $query, $select) {
219219
return function ($q) use ($field, $locale, $query, $select) {
220220
/* @var \Cake\Datasource\QueryInterface $q */
221-
$q->where([$q->repository()->aliasField('locale') => $locale]);
221+
$q->where([$q->getRepository()->aliasField('locale') => $locale]);
222222

223223
/* @var \Cake\ORM\Query $query */
224224
if ($query->isAutoFieldsEnabled() ||

src/ORM/EagerLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ protected function _resolveJoins($associations, $matching = [])
671671
*/
672672
public function loadExternal($query, $statement)
673673
{
674-
$external = $this->externalAssociations($query->repository());
674+
$external = $this->externalAssociations($query->getRepository());
675675
if (empty($external)) {
676676
return $statement;
677677
}

src/ORM/Query.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,11 @@ public function contain($associations = null, $override = false)
444444
if ($associations) {
445445
$loader->contain($associations, $queryBuilder);
446446
}
447-
$this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $loader->getContain());
447+
$this->_addAssociationsToTypeMap(
448+
$this->getRepository(),
449+
$this->getTypeMap(),
450+
$loader->getContain()
451+
);
448452

449453
return $this;
450454
}
@@ -551,7 +555,7 @@ protected function _addAssociationsToTypeMap($table, $typeMap, $associations)
551555
public function matching($assoc, callable $builder = null)
552556
{
553557
$result = $this->getEagerLoader()->setMatching($assoc, $builder)->getMatching();
554-
$this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $result);
558+
$this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result);
555559
$this->_dirty();
556560

557561
return $this;
@@ -628,7 +632,7 @@ public function leftJoinWith($assoc, callable $builder = null)
628632
'fields' => false
629633
])
630634
->getMatching();
631-
$this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $result);
635+
$this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result);
632636
$this->_dirty();
633637

634638
return $this;
@@ -677,7 +681,7 @@ public function innerJoinWith($assoc, callable $builder = null)
677681
'fields' => false
678682
])
679683
->getMatching();
680-
$this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $result);
684+
$this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result);
681685
$this->_dirty();
682686

683687
return $this;
@@ -742,7 +746,7 @@ public function notMatching($assoc, callable $builder = null)
742746
'negateMatch' => true
743747
])
744748
->getMatching();
745-
$this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $result);
749+
$this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result);
746750
$this->_dirty();
747751

748752
return $this;
@@ -1051,7 +1055,7 @@ public function all()
10511055
public function triggerBeforeFind()
10521056
{
10531057
if (!$this->_beforeFindFired && $this->_type === 'select') {
1054-
$table = $this->repository();
1058+
$table = $this->getRepository();
10551059
$this->_beforeFindFired = true;
10561060
/* @var \Cake\Event\EventDispatcherInterface $table */
10571061
$table->dispatchEvent('Model.beforeFind', [
@@ -1134,11 +1138,11 @@ protected function _addDefaultFields()
11341138

11351139
if (!count($select) || $this->_autoFields === true) {
11361140
$this->_hasFields = false;
1137-
$this->select($this->repository()->getSchema()->columns());
1141+
$this->select($this->getRepository()->getSchema()->columns());
11381142
$select = $this->clause('select');
11391143
}
11401144

1141-
$aliased = $this->aliasFields($select, $this->repository()->getAlias());
1145+
$aliased = $this->aliasFields($select, $this->getRepository()->getAlias());
11421146
$this->select($aliased, true);
11431147
}
11441148

@@ -1175,7 +1179,7 @@ protected function _addDefaultSelectTypes()
11751179
*/
11761180
public function find($finder, array $options = [])
11771181
{
1178-
return $this->repository()->callFinder($finder, $this, $options);
1182+
return $this->getRepository()->callFinder($finder, $this, $options);
11791183
}
11801184

11811185
/**
@@ -1202,7 +1206,7 @@ protected function _dirty()
12021206
*/
12031207
public function update($table = null)
12041208
{
1205-
$table = $table ?: $this->repository()->getTable();
1209+
$table = $table ?: $this->getRepository()->getTable();
12061210

12071211
return parent::update($table);
12081212
}
@@ -1218,7 +1222,7 @@ public function update($table = null)
12181222
*/
12191223
public function delete($table = null)
12201224
{
1221-
$repo = $this->repository();
1225+
$repo = $this->getRepository();
12221226
$this->from([$repo->getAlias() => $repo->getTable()]);
12231227

12241228
return parent::delete();
@@ -1239,7 +1243,7 @@ public function delete($table = null)
12391243
*/
12401244
public function insert(array $columns, array $types = [])
12411245
{
1242-
$table = $this->repository()->getTable();
1246+
$table = $this->getRepository()->getTable();
12431247
$this->into($table);
12441248

12451249
return parent::insert($columns, $types);

src/ORM/ResultSet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ class ResultSet implements ResultSetInterface
175175
*/
176176
public function __construct($query, $statement)
177177
{
178-
$repository = $query->repository();
178+
$repository = $query->getRepository();
179179
$this->_statement = $statement;
180180
$this->_driver = $query->getConnection()->getDriver();
181-
$this->_defaultTable = $query->repository();
181+
$this->_defaultTable = $query->getRepository();
182182
$this->_calculateAssociationMap($query);
183183
$this->_hydrate = $query->isHydrationEnabled();
184184
$this->_entityClass = $repository->getEntityClass();

tests/TestCase/Controller/Component/PaginatorComponentTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,9 @@ protected function _getMockFindQuery($table = null)
14051405
->method('count')
14061406
->will($this->returnValue(2));
14071407

1408-
$query->repository($table);
1408+
if ($table) {
1409+
$query->repository($table);
1410+
}
14091411

14101412
return $query;
14111413
}

tests/TestCase/Datasource/PaginatorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,9 @@ protected function _getMockFindQuery($table = null)
13801380
->method('count')
13811381
->will($this->returnValue(2));
13821382

1383-
$query->repository($table);
1383+
if ($table) {
1384+
$query->repository($table);
1385+
}
13841386

13851387
return $query;
13861388
}

tests/TestCase/ORM/Association/HasManyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public function testEagerLoaderMultipleKeys()
440440
$keys = [[1, 10], [2, 20], [3, 30], [4, 40]];
441441
$query = $this->getMockBuilder('Cake\ORM\Query')
442442
->setMethods(['all', 'andWhere'])
443-
->setConstructorArgs([null, null])
443+
->disableOriginalConstructor()
444444
->getMock();
445445
$this->article->method('find')
446446
->with('all')

tests/TestCase/ORM/Association/HasOneTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function testAttachToMultiPrimaryKey()
186186

187187
$query = $this->getMockBuilder('\Cake\ORM\Query')
188188
->setMethods(['join', 'select'])
189-
->setConstructorArgs([null, null])
189+
->disableOriginalConstructor()
190190
->getMock();
191191
$field1 = new IdentifierExpression('Profiles.user_id');
192192
$field2 = new IdentifierExpression('Profiles.user_site_id');
@@ -216,7 +216,7 @@ public function testAttachToMultiPrimaryKeyMismatch()
216216
$this->expectExceptionMessage('Cannot match provided foreignKey for "Profiles", got "(user_id)" but expected foreign key for "(id, site_id)"');
217217
$query = $this->getMockBuilder('\Cake\ORM\Query')
218218
->setMethods(['join', 'select'])
219-
->setConstructorArgs([null, null])
219+
->disableOriginalConstructor()
220220
->getMock();
221221
$config = [
222222
'sourceTable' => $this->user,

tests/TestCase/ORM/QueryTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ public function strategiesProviderBelongsToMany()
138138
return [['subquery'], ['select']];
139139
}
140140

141+
/**
142+
* Test getRepository() method.
143+
*
144+
* @return void
145+
*/
146+
public function testGetRepository()
147+
{
148+
$query = new Query($this->connection, $this->table);
149+
150+
$result = $query->getRepository();
151+
$this->assertSame($this->table, $result);
152+
}
153+
141154
/**
142155
* Tests that results are grouped correctly when using contain()
143156
* and results are not hydrated

0 commit comments

Comments
 (0)