Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use strict tryLoadOne in ContainsOne #848

Merged
merged 3 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -806,14 +806,14 @@ field only to offer payments made by the same client. Inside Model_Invoice add::
$m = new Model_Invoice($db);
$m->set('client_id', 123);

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

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

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

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

});
Expand Down
2 changes: 1 addition & 1 deletion docs/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ database at once::
$m->addExpression('total_payments', (new Model_Payment($db))->action('count'));
$m->addExpression('total_received', (new Model_Payment($db))->action('fx0', ['sum', 'amount']));

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

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

Expand Down
13 changes: 0 additions & 13 deletions docs/persistence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,6 @@ There are several ways to link your model up with the persistence::

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

.. php:method:: loadAny

Attempt to load any matching record. You can use this in conjunction with
setOrder()::

$m->loadAny();
echo $m->get('name');

.. php:method:: tryLoadAny

Attempt to load any record, but silently fail if there are no records in
the DataSet.

.. php:method:: unload

Remove active record and restore model to default state::
Expand Down
3 changes: 1 addition & 2 deletions src/Reference/ContainsOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ public function ref(array $defaults = []): Model
});
}

// try to load any (actually only one possible) record
$theirModel->tryLoadAny();
$theirModel->tryLoadOne();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change


return $theirModel;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function testDateCondition2()
$m->addField('date', ['type' => 'date']);

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

Expand Down
6 changes: 3 additions & 3 deletions tests/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function testBasic()
['name' => 'tools', 'qty' => 5, 'price' => 10],
['name' => 'work', 'qty' => 1, 'price' => 40],
]]);
$quote->loadAny();
$quote->loadOne();

// total price should match
$this->assertEquals(90.00, $quote->get('total'));
Expand Down Expand Up @@ -275,7 +275,7 @@ public function testError()
['name' => 'tools', 'qty' => 5, 'price' => 10],
['name' => 'work', 'qty' => 1, 'price' => 40],
]]);
$quote->loadAny();
$quote->loadOne();

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

$invoice = new DcInvoice();
$invoice->onHook(DeepCopy::HOOK_AFTER_COPY, static function ($m) {
Expand Down
4 changes: 2 additions & 2 deletions tests/ExpressionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testNakedExpression()
$db = new Persistence\Sql($this->db->connection);
$m = new Model($db, ['table' => false]);
$m->addExpression('x', '2+3');
$m->tryLoadAny();
$m->loadOne();
$this->assertEquals(5, $m->get('x'));
}

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

// normal fields
$this->assertSame(0, $i->get('zero_basic'));
Expand Down
4 changes: 2 additions & 2 deletions tests/ReferenceSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public function testBasic2()
$u->hasMany('cur', ['model' => $c, 'our_field' => 'currency', 'their_field' => 'currency']);

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

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testCondition()

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

$user->loadAny();
$user->loadOne();

$this->assertEquals('Smith', $user->get('surname'));
}
Expand Down
16 changes: 8 additions & 8 deletions tests/TypecastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public function testLoadBy()
$m = new Model($db, ['table' => 'types']);
$m->addField('date', ['type' => 'date', 'dateTimeClass' => MyDate::class]);

$m->loadAny();
$m->loadOne();
$this->assertTrue($m->loaded());
$d = $m->get('date');
$m->unload();
Expand All @@ -386,7 +386,7 @@ public function testLoadBy()
$this->assertTrue($m->loaded());
$m->unload();

$m->addCondition('date', $d)->loadAny();
$m->addCondition('date', $d)->loadOne();
$this->assertTrue($m->loaded());
}

Expand Down Expand Up @@ -453,7 +453,7 @@ public function testTimestamp()

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

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

public function testDirtyTimestamp()
Expand All @@ -493,7 +493,7 @@ public function testDirtyTimestamp()

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

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

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

Expand Down Expand Up @@ -575,7 +575,7 @@ public function testDirtyTime()

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

$m->set('ts', $sql_time_new);
$this->assertTrue($m->isDirty('ts'));
Expand Down Expand Up @@ -603,7 +603,7 @@ public function testDirtyTimeAfterSave()

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

$m->set('ts', $sql_time);
$this->assertTrue($m->isDirty('ts'));
Expand Down