Skip to content

Commit 2b6dc2d

Browse files
authored
Use strict tryLoadOne in ContainsOne (#848)
* fix docs * Use strict tryLoadOne in ContainsOne * improve tests
1 parent ebb69d4 commit 2b6dc2d

10 files changed

+21
-35
lines changed

docs/advanced.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -806,14 +806,14 @@ field only to offer payments made by the same client. Inside Model_Invoice add::
806806
$m = new Model_Invoice($db);
807807
$m->set('client_id', 123);
808808

809-
$m->set('payment_invoice_id', $m->ref('payment_invoice_id')->tryLoadAny()->getId());
809+
$m->set('payment_invoice_id', $m->ref('payment_invoice_id')->tryLoadOne()->getId());
810810

811811
In this case the payment_invoice_id will be set to ID of any payment by client
812812
123. There also may be some better uses::
813813

814814
$cl->ref('Invoice')->each(function($m) {
815815

816-
$m->set('payment_invoice_id', $m->ref('payment_invoice_id')->tryLoadAny()->getId());
816+
$m->set('payment_invoice_id', $m->ref('payment_invoice_id')->tryLoadOne()->getId());
817817
$m->save();
818818

819819
});

docs/expressions.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ database at once::
8484
$m->addExpression('total_payments', (new Model_Payment($db))->action('count'));
8585
$m->addExpression('total_received', (new Model_Payment($db))->action('fx0', ['sum', 'amount']));
8686

87-
$data = $m->loadAny()->get();
87+
$data = $m->loadOne()->get();
8888

8989
Of course you can also use a DSQL for this::
9090

docs/persistence.rst

-13
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,6 @@ There are several ways to link your model up with the persistence::
7373

7474
$m->save(); // will either create new record or update existing
7575

76-
.. php:method:: loadAny
77-
78-
Attempt to load any matching record. You can use this in conjunction with
79-
setOrder()::
80-
81-
$m->loadAny();
82-
echo $m->get('name');
83-
84-
.. php:method:: tryLoadAny
85-
86-
Attempt to load any record, but silently fail if there are no records in
87-
the DataSet.
88-
8976
.. php:method:: unload
9077
9178
Remove active record and restore model to default state::

src/Reference/ContainsOne.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ public function ref(array $defaults = []): Model
100100
});
101101
}
102102

103-
// try to load any (actually only one possible) record
104-
$theirModel->tryLoadAny();
103+
$theirModel->tryLoadOne();
105104

106105
return $theirModel;
107106
}

tests/ConditionSqlTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public function testDateCondition2()
338338
$m->addField('date', ['type' => 'date']);
339339

340340
$m->addCondition('date', new \DateTime('08-12-1982'));
341-
$m->loadAny();
341+
$m->loadOne();
342342
$this->assertSame('Sue', $m->get('name'));
343343
}
344344

tests/DeepCopyTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function testBasic()
162162
['name' => 'tools', 'qty' => 5, 'price' => 10],
163163
['name' => 'work', 'qty' => 1, 'price' => 40],
164164
]]);
165-
$quote->loadAny();
165+
$quote->loadOne();
166166

167167
// total price should match
168168
$this->assertEquals(90.00, $quote->get('total'));
@@ -275,7 +275,7 @@ public function testError()
275275
['name' => 'tools', 'qty' => 5, 'price' => 10],
276276
['name' => 'work', 'qty' => 1, 'price' => 40],
277277
]]);
278-
$quote->loadAny();
278+
$quote->loadOne();
279279

280280
$invoice = new DcInvoice();
281281
$invoice->onHook(DeepCopy::HOOK_AFTER_COPY, static function ($m) {
@@ -316,7 +316,7 @@ public function testDeepError()
316316
['name' => 'tools', 'qty' => 5, 'price' => 10],
317317
['name' => 'work', 'qty' => 1, 'price' => 40],
318318
]]);
319-
$quote->loadAny();
319+
$quote->loadOne();
320320

321321
$invoice = new DcInvoice();
322322
$invoice->onHook(DeepCopy::HOOK_AFTER_COPY, static function ($m) {

tests/ExpressionSqlTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testNakedExpression()
1717
$db = new Persistence\Sql($this->db->connection);
1818
$m = new Model($db, ['table' => false]);
1919
$m->addExpression('x', '2+3');
20-
$m->tryLoadAny();
20+
$m->loadOne();
2121
$this->assertEquals(5, $m->get('x'));
2222
}
2323

@@ -254,7 +254,7 @@ public function testNeverSaveNeverPersist()
254254
$i->addExpression('one_basic', [$i->expr('1'), 'type' => 'integer', 'system' => true]);
255255
$i->addExpression('one_never_save', [$i->expr('1'), 'type' => 'integer', 'system' => true, 'never_save' => true]);
256256
$i->addExpression('one_never_persist', [$i->expr('1'), 'type' => 'integer', 'system' => true, 'never_persist' => true]);
257-
$i->loadAny();
257+
$i->loadOne();
258258

259259
// normal fields
260260
$this->assertSame(0, $i->get('zero_basic'));

tests/ReferenceSqlTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ public function testBasic2()
9696
$u->hasMany('cur', ['model' => $c, 'our_field' => 'currency', 'their_field' => 'currency']);
9797

9898
$cc = (clone $u)->load(1)->ref('cur');
99-
$cc->tryLoadAny();
99+
$cc->tryLoadOne();
100100
$this->assertSame('Euro', $cc->get('name'));
101101

102102
$cc = (clone $u)->load(2)->ref('cur');
103-
$cc->tryLoadAny();
103+
$cc->tryLoadOne();
104104
$this->assertSame('Pound', $cc->get('name'));
105105
}
106106

tests/ScopeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testCondition()
129129

130130
$user->scope()->add($condition);
131131

132-
$user->loadAny();
132+
$user->loadOne();
133133

134134
$this->assertEquals('Smith', $user->get('surname'));
135135
}

tests/TypecastingTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public function testLoadBy()
377377
$m = new Model($db, ['table' => 'types']);
378378
$m->addField('date', ['type' => 'date', 'dateTimeClass' => MyDate::class]);
379379

380-
$m->loadAny();
380+
$m->loadOne();
381381
$this->assertTrue($m->loaded());
382382
$d = $m->get('date');
383383
$m->unload();
@@ -386,7 +386,7 @@ public function testLoadBy()
386386
$this->assertTrue($m->loaded());
387387
$m->unload();
388388

389-
$m->addCondition('date', $d)->loadAny();
389+
$m->addCondition('date', $d)->loadOne();
390390
$this->assertTrue($m->loaded());
391391
}
392392

@@ -453,7 +453,7 @@ public function testTimestamp()
453453

454454
$m = new Model($db, ['table' => 'types']);
455455
$m->addField('ts', ['actual' => 'date', 'type' => 'datetime']);
456-
$m->loadAny();
456+
$m->loadOne();
457457

458458
// must respect 'actual'
459459
$this->assertNotNull($m->get('ts'));
@@ -475,7 +475,7 @@ public function testBadTimestamp()
475475
$m = new Model($db, ['table' => 'types']);
476476
$m->addField('ts', ['actual' => 'date', 'type' => 'datetime']);
477477
$this->expectException(Exception::class);
478-
$m->loadAny();
478+
$m->loadOne();
479479
}
480480

481481
public function testDirtyTimestamp()
@@ -493,7 +493,7 @@ public function testDirtyTimestamp()
493493

494494
$m = new Model($db, ['table' => 'types']);
495495
$m->addField('ts', ['actual' => 'date', 'type' => 'datetime']);
496-
$m->loadAny();
496+
$m->loadOne();
497497
$m->set('ts', clone $m->get('ts'));
498498

499499
$this->assertFalse($m->isDirty('ts'));
@@ -512,7 +512,7 @@ public function testTimestampSave()
512512

513513
$m = new Model($db, ['table' => 'types']);
514514
$m->addField('ts', ['actual' => 'date', 'type' => 'date']);
515-
$m->loadAny();
515+
$m->loadOne();
516516
$m->set('ts', new \DateTime('2012-02-30'));
517517
$m->save();
518518

@@ -575,7 +575,7 @@ public function testDirtyTime()
575575

576576
$m = new Model($db, ['table' => 'types']);
577577
$m->addField('ts', ['actual' => 'date', 'type' => 'time']);
578-
$m->loadAny();
578+
$m->loadOne();
579579

580580
$m->set('ts', $sql_time_new);
581581
$this->assertTrue($m->isDirty('ts'));
@@ -603,7 +603,7 @@ public function testDirtyTimeAfterSave()
603603

604604
$m = new Model($db, ['table' => 'types']);
605605
$m->addField('ts', ['actual' => 'date', 'type' => 'time']);
606-
$m->loadAny();
606+
$m->loadOne();
607607

608608
$m->set('ts', $sql_time);
609609
$this->assertTrue($m->isDirty('ts'));

0 commit comments

Comments
 (0)