Skip to content

Commit 352915a

Browse files
Divide one() method in two. (#239)
1 parent 60ec3d8 commit 352915a

12 files changed

Lines changed: 110 additions & 95 deletions

src/ActiveQuery.php

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,6 @@ public function __construct(
115115
parent::__construct($db);
116116
}
117117

118-
/**
119-
* Executes query and returns all results as an array.
120-
*
121-
* If null, the DB connection returned by {@see arClass} will be used.
122-
*
123-
* @throws Exception
124-
* @throws InvalidConfigException
125-
* @throws Throwable
126-
*
127-
* @return array the query results. If the query results in nothing, an empty array will be returned.
128-
*
129-
* @psalm-return ActiveRecord[]|array
130-
*/
131-
public function all(): array
132-
{
133-
return parent::all();
134-
}
135-
136118
public function prepare(QueryBuilderInterface $builder): QueryInterface
137119
{
138120
/**
@@ -197,11 +179,11 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface
197179
}
198180
} else {
199181
if ($viaCallableUsed) {
200-
$model = $viaQuery->one();
182+
$model = $viaQuery->onePopulate();
201183
} elseif ($this->primaryModel->isRelationPopulated($viaName)) {
202184
$model = $this->primaryModel->$viaName;
203185
} else {
204-
$model = $viaQuery->one();
186+
$model = $viaQuery->onePopulate();
205187
$this->primaryModel->populateRelation($viaName, $model);
206188
}
207189
$viaModels = $model === null ? [] : [$model];
@@ -344,12 +326,20 @@ private function removeDuplicatedModels(array $models): array
344326
return array_values($models);
345327
}
346328

347-
/**
348-
* @psalm-suppress NullableReturnStatement
349-
*/
350-
public function one(): array|null|object
329+
public function allPopulate(): array
330+
{
331+
$rows = $this->all();
332+
333+
if ($rows !== []) {
334+
$rows = $this->populate($rows);
335+
}
336+
337+
return $rows;
338+
}
339+
340+
public function onePopulate(): array|ActiveRecordInterface|null
351341
{
352-
$row = parent::one();
342+
$row = $this->one();
353343

354344
if ($row !== null) {
355345
$activeRecord = $this->populate([$row]);
@@ -1031,7 +1021,7 @@ public function getARClass(): string|null
10311021
*/
10321022
public function findOne(mixed $condition): array|ActiveRecordInterface|null
10331023
{
1034-
return $this->findByCondition($condition)->one();
1024+
return $this->findByCondition($condition)->onePopulate();
10351025
}
10361026

10371027
/**
@@ -1063,7 +1053,7 @@ public function findAll(mixed $condition): array
10631053
* @throws NotFoundException
10641054
* @throws NotInstantiableException
10651055
*/
1066-
protected function findByCondition(mixed $condition): QueryInterface
1056+
protected function findByCondition(mixed $condition): static
10671057
{
10681058
$arInstance = $this->getARInstance();
10691059

@@ -1114,10 +1104,8 @@ protected function findByCondition(mixed $condition): QueryInterface
11141104
*
11151105
* @param string $sql the SQL statement to be executed.
11161106
* @param array $params parameters to be bound to the SQL statement during execution.
1117-
*
1118-
* @return QueryInterface the newly created {@see ActiveQuery} instance
11191107
*/
1120-
public function findBySql(string $sql, array $params = []): QueryInterface
1108+
public function findBySql(string $sql, array $params = []): self
11211109
{
11221110
return $this->sql($sql)->params($params);
11231111
}

src/ActiveQueryInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,16 @@ public function link(array $value): self;
277277
* If false, only the first row of the results will be retrieved using {@see Query::one()|one()}.
278278
*/
279279
public function multiple(bool $value): self;
280+
281+
/**
282+
* Executes the query and returns ActiveRecord instances populated with the query result.
283+
*
284+
* @return array the query results. If the query results in nothing, an empty array will be returned.
285+
*/
286+
public function allPopulate(): array|ActiveRecordInterface|null;
287+
288+
/**
289+
* Executes the query and returns ActiveRecord instances populated with the query result.
290+
*/
291+
public function onePopulate(): array|ActiveRecordInterface|null;
280292
}

src/ActiveRecord.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function refresh(): bool
298298

299299
$query->where($pk);
300300

301-
return $this->refreshInternal($query->one());
301+
return $this->refreshInternal($query->onePopulate());
302302
}
303303

304304
/**

src/ActiveRelationTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function findFor(string $name, ActiveRecordInterface $model): array|null|
199199
}
200200
}
201201

202-
return $this->multiple ? $this->all() : $this->one();
202+
return $this->multiple ? $this->all() : $this->onePopulate();
203203
}
204204

205205
/**
@@ -271,7 +271,7 @@ public function populateRelation(string $name, array &$primaryModels): array
271271
}
272272

273273
if (!$this->multiple && count($primaryModels) === 1) {
274-
$model = $this->one();
274+
$model = $this->onePopulate();
275275
$primaryModel = reset($primaryModels);
276276

277277
if ($primaryModel instanceof ActiveRecordInterface) {
@@ -714,8 +714,8 @@ public function getLink(): array
714714
}
715715

716716
/**
717-
* @return ActiveQueryInterface|array|null the query associated with the junction table. Please call {@see (via)()} to
718-
* set this property instead of directly setting it.
717+
* @return ActiveQueryInterface|array|null the query associated with the junction table.
718+
* Please call {@see (via)()} to set this property instead of directly setting it.
719719
*
720720
* This property is only used in relational context.
721721
*

tests/ActiveQueryFindTest.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,19 @@ public function testFindBySql(): void
7777

7878
$customerQuery = new ActiveQuery(Customer::class, $this->db);
7979

80-
/** find one */
81-
$customers = $customerQuery->findBySql('SELECT * FROM {{customer}} ORDER BY [[id]] DESC')->one();
80+
/** find onePopulate */
81+
$customers = $customerQuery->findBySql('SELECT * FROM {{customer}} ORDER BY [[id]] DESC')->onePopulate();
8282
$this->assertInstanceOf(Customer::class, $customers);
8383
$this->assertEquals('user3', $customers->getAttribute('name'));
8484

85-
/** find all */
86-
$customers = $customerQuery->findBySql('SELECT * FROM {{customer}}')->all();
85+
/** find allPopulate */
86+
$customers = $customerQuery->findBySql('SELECT * FROM {{customer}}')->allPopulate();
8787
$this->assertCount(3, $customers);
8888

8989
/** find with parameter binding */
90-
$customers = $customerQuery->findBySql('SELECT * FROM {{customer}} WHERE [[id]]=:id', [':id' => 2])->one();
90+
$customers = $customerQuery
91+
->findBySql('SELECT * FROM {{customer}} WHERE [[id]]=:id', [':id' => 2])
92+
->onePopulate();
9193
$this->assertInstanceOf(Customer::class, $customers);
9294
$this->assertEquals('user2', $customers->getAttribute('name'));
9395
}
@@ -208,12 +210,12 @@ public function testFind(): void
208210
$this->assertInstanceOf(ActiveQueryInterface::class, $customerQuery);
209211

210212
/** find one */
211-
$customer = $customerQuery->one();
213+
$customer = $customerQuery->onePopulate();
212214
$this->assertInstanceOf(Customer::class, $customer);
213215

214216
/** find all */
215217
$customerQuery = new ActiveQuery(Customer::class, $this->db);
216-
$customers = $customerQuery->all();
218+
$customers = $customerQuery->allPopulate();
217219
$this->assertCount(3, $customers);
218220
$this->assertInstanceOf(Customer::class, $customers[0]);
219221
$this->assertInstanceOf(Customer::class, $customers[1]);
@@ -252,13 +254,13 @@ public function testFind(): void
252254

253255
/** find by attributes */
254256
$customerQuery = new ActiveQuery(Customer::class, $this->db);
255-
$customer = $customerQuery->where(['name' => 'user2'])->one();
257+
$customer = $customerQuery->where(['name' => 'user2'])->onePopulate();
256258
$this->assertInstanceOf(Customer::class, $customer);
257259
$this->assertEquals(2, $customer->id);
258260

259261
/** scope */
260262
$customerQuery = new CustomerQuery(Customer::class, $this->db);
261-
$this->assertCount(2, $customerQuery->active()->all());
263+
$this->assertCount(2, $customerQuery->active()->allPopulate());
262264
$this->assertEquals(2, $customerQuery->active()->count());
263265
}
264266

@@ -391,48 +393,48 @@ public function testFindLimit(): void
391393

392394
/** one */
393395
$customerQuery = new ActiveQuery(Customer::class, $this->db);
394-
$customer = $customerQuery->orderBy('id')->one();
396+
$customer = $customerQuery->orderBy('id')->onePopulate();
395397
$this->assertEquals('user1', $customer->name);
396398

397399
/** all */
398400
$customerQuery = new ActiveQuery(Customer::class, $this->db);
399-
$customers = $customerQuery->all();
401+
$customers = $customerQuery->allPopulate();
400402
$this->assertCount(3, $customers);
401403

402404
/** limit */
403405
$customerQuery = new ActiveQuery(Customer::class, $this->db);
404-
$customers = $customerQuery->orderBy('id')->limit(1)->all();
406+
$customers = $customerQuery->orderBy('id')->limit(1)->allPopulate();
405407
$this->assertCount(1, $customers);
406408
$this->assertEquals('user1', $customers[0]->name);
407409

408-
$customers = $customerQuery->orderBy('id')->limit(1)->offset(1)->all();
410+
$customers = $customerQuery->orderBy('id')->limit(1)->offset(1)->allPopulate();
409411
$this->assertCount(1, $customers);
410412
$this->assertEquals('user2', $customers[0]->name);
411413

412-
$customers = $customerQuery->orderBy('id')->limit(1)->offset(2)->all();
414+
$customers = $customerQuery->orderBy('id')->limit(1)->offset(2)->allPopulate();
413415
$this->assertCount(1, $customers);
414416
$this->assertEquals('user3', $customers[0]->name);
415417

416-
$customers = $customerQuery->orderBy('id')->limit(2)->offset(1)->all();
418+
$customers = $customerQuery->orderBy('id')->limit(2)->offset(1)->allPopulate();
417419
$this->assertCount(2, $customers);
418420
$this->assertEquals('user2', $customers[0]->name);
419421
$this->assertEquals('user3', $customers[1]->name);
420422

421-
$customers = $customerQuery->limit(2)->offset(3)->all();
423+
$customers = $customerQuery->limit(2)->offset(3)->allPopulate();
422424
$this->assertCount(0, $customers);
423425

424426
/** offset */
425427
$customerQuery = new ActiveQuery(Customer::class, $this->db);
426-
$customer = $customerQuery->orderBy('id')->offset(0)->one();
428+
$customer = $customerQuery->orderBy('id')->offset(0)->onePopulate();
427429
$this->assertEquals('user1', $customer->name);
428430

429-
$customer = $customerQuery->orderBy('id')->offset(1)->one();
431+
$customer = $customerQuery->orderBy('id')->offset(1)->onePopulate();
430432
$this->assertEquals('user2', $customer->name);
431433

432-
$customer = $customerQuery->orderBy('id')->offset(2)->one();
434+
$customer = $customerQuery->orderBy('id')->offset(2)->onePopulate();
433435
$this->assertEquals('user3', $customer->name);
434436

435-
$customer = $customerQuery->offset(3)->one();
437+
$customer = $customerQuery->offset(3)->onePopulate();
436438
$this->assertNull($customer);
437439
}
438440

@@ -507,7 +509,7 @@ public function testFindEager(): void
507509
unset($customers[1]->orders);
508510
$this->assertFalse($customers[1]->isRelationPopulated('orders'));
509511

510-
$customer = $customerQuery->where(['id' => 1])->with('orders')->one();
512+
$customer = $customerQuery->where(['id' => 1])->with('orders')->onePopulate();
511513
$this->assertTrue($customer->isRelationPopulated('orders'));
512514
$this->assertCount(1, $customer->orders);
513515
$this->assertCount(1, $customer->relatedRecords);
@@ -563,7 +565,7 @@ public function testFindNestedRelation(): void
563565
$this->assertCount(3, $customers[2]->orders[0]->items);
564566
$this->assertCount(1, $customers[2]->orders[1]->items);
565567

566-
$customers = $customerQuery->where(['id' => 1])->with('ordersWithItems')->one();
568+
$customers = $customerQuery->where(['id' => 1])->with('ordersWithItems')->onePopulate();
567569
$this->assertTrue($customers->isRelationPopulated('ordersWithItems'));
568570
$this->assertCount(1, $customers->ordersWithItems);
569571

@@ -661,15 +663,15 @@ public function testFindEagerIndexBy(): void
661663
$this->checkFixture($this->db, 'order');
662664

663665
$orderQuery = new ActiveQuery(Order::class, $this->db);
664-
$order = $orderQuery->with('itemsIndexed')->where(['id' => 1])->one();
666+
$order = $orderQuery->with('itemsIndexed')->where(['id' => 1])->onePopulate();
665667
$this->assertTrue($order->isRelationPopulated('itemsIndexed'));
666668

667669
$items = $order->itemsIndexed;
668670
$this->assertCount(2, $items);
669671
$this->assertTrue(isset($items[1]));
670672
$this->assertTrue(isset($items[2]));
671673

672-
$order = $orderQuery->with('itemsIndexed')->where(['id' => 2])->one();
674+
$order = $orderQuery->with('itemsIndexed')->where(['id' => 2])->onePopulate();
673675
$this->assertTrue($order->isRelationPopulated('itemsIndexed'));
674676

675677
$items = $order->itemsIndexed;

0 commit comments

Comments
 (0)