Skip to content

Commit c0764c8

Browse files
committed
Added second signature for contain that matches the signature for matching
1 parent 0c5c24c commit c0764c8

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

src/ORM/EagerLoader.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,23 @@ class EagerLoader
126126
* @param array|string $associations list of table aliases to be queried.
127127
* When this method is called multiple times it will merge previous list with
128128
* the new one.
129+
* @param callable|null $queryBuilder The query builder callable
129130
* @return array Containments.
130131
*/
131-
public function contain($associations = [])
132+
public function contain($associations = [], callable $queryBuilder = null)
132133
{
133134
if (empty($associations)) {
134135
return $this->_containments;
135136
}
136137

138+
if ($queryBuilder) {
139+
$associations = [
140+
$associations => [
141+
'queryBuilder' => $queryBuilder
142+
]
143+
];
144+
}
145+
137146
$associations = (array)$associations;
138147
$associations = $this->_reformatContain($associations, $this->_containments);
139148
$this->_normalized = null;

src/ORM/Query.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,21 @@ public function eagerLoader(EagerLoader $instance = null)
352352
* Failing to do so will trigger exceptions.
353353
*
354354
* ```
355-
* // Use special join conditions for getting an Articles's belongsTo 'authors'
355+
* // Use a query builder to add conditions to the containment
356+
* $query->contain('Authors', function ($q) {
357+
* return $q->where(...); // add conditions
358+
* });
359+
* // Use special join conditions for multiple containments in the same method call
356360
* $query->contain([
357361
* 'Authors' => [
358362
* 'foreignKey' => false,
359363
* 'queryBuilder' => function ($q) {
360364
* return $q->where(...); // Add full filtering conditions
361365
* }
362-
* ]
366+
* ],
367+
* 'Tags' => function ($q) {
368+
* return $q->where(...); // add conditions
369+
* }
363370
* ]);
364371
* ```
365372
*
@@ -371,7 +378,9 @@ public function eagerLoader(EagerLoader $instance = null)
371378
* previous list will be emptied.
372379
*
373380
* @param array|string|null $associations List of table aliases to be queried.
374-
* @param bool $override Whether override previous list with the one passed
381+
* @param callable|bool $override The query builder for the association, or
382+
* if associations is an array, a bool on whether to override previous list
383+
* with the one passed
375384
* defaults to merging previous list with the new one.
376385
* @return array|$this
377386
*/
@@ -386,7 +395,12 @@ public function contain($associations = null, $override = false)
386395
return $loader->contain();
387396
}
388397

389-
$result = $loader->contain($associations);
398+
$queryBuilder = null;
399+
if (is_callable($override)) {
400+
$queryBuilder = $override;
401+
}
402+
403+
$result = $loader->contain($associations, $queryBuilder);
390404
$this->_addAssociationsToTypeMap($this->repository(), $this->getTypeMap(), $result);
391405

392406
return $this;

tests/TestCase/ORM/EagerLoaderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,26 @@ public function testContainClosure()
332332
$this->assertEquals($expected, $loader->contain());
333333
}
334334

335+
/**
336+
* Tests using the same signature as matching with contain
337+
*
338+
* @return void
339+
*/
340+
public function testContainSecondSignature()
341+
{
342+
$builder = function ($query) {
343+
};
344+
$loader = new EagerLoader;
345+
$loader->contain('clients', $builder);
346+
347+
$expected = [
348+
'clients' => [
349+
'queryBuilder' => $builder
350+
]
351+
];
352+
$this->assertEquals($expected, $loader->contain());
353+
}
354+
335355
/**
336356
* Tests that query builders are stacked
337357
*

tests/TestCase/ORM/QueryTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,32 @@ public function testContainWithClosure()
20182018
$this->assertEquals([1], array_unique($ids));
20192019
}
20202020

2021+
/**
2022+
* Integration test that uses the contain signature that is the same as the
2023+
* matching signature
2024+
*
2025+
* @return void
2026+
*/
2027+
public function testContainSecondSignature()
2028+
{
2029+
$table = TableRegistry::get('authors');
2030+
$table->hasMany('articles');
2031+
$query = new Query($this->connection, $table);
2032+
$query
2033+
->select()
2034+
->contain('articles', function ($q) {
2035+
return $q->where(['articles.id' => 1]);
2036+
});
2037+
2038+
$ids = [];
2039+
foreach ($query as $entity) {
2040+
foreach ((array)$entity->articles as $article) {
2041+
$ids[] = $article->id;
2042+
}
2043+
}
2044+
$this->assertEquals([1], array_unique($ids));
2045+
}
2046+
20212047
/**
20222048
* Integration test to ensure that filtering associations with the queryBuilder
20232049
* option works.

0 commit comments

Comments
 (0)