Skip to content

Commit b4d8de5

Browse files
authored
Deprecate Model::loaded in favor of Model::isLoaded (#931)
1 parent 7a05aa8 commit b4d8de5

20 files changed

+75
-72
lines changed

docs/advanced.rst

+12-20
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ which I want to define like this::
158158
$this->getOwner()->addField('created_dts', ['type' => 'datetime', 'default' => new \DateTime()]);
159159

160160
$this->getOwner()->hasOne('created_by_user_id', 'User');
161-
if(isset($this->getApp()->user) && $this->getApp()->user->loaded()) {
161+
if(isset($this->getApp()->user) && $this->getApp()->user->isLoaded()) {
162162
$this->getOwner()->getField('created_by_user_id')->default = $this->getApp()->user->getId();
163163
}
164164

@@ -167,7 +167,7 @@ which I want to define like this::
167167
$this->getOwner()->addField('updated_dts', ['type' => 'datetime']);
168168

169169
$this->getOwner()->onHook(Model::HOOK_BEFORE_UPDATE, function($m, $data) {
170-
if(isset($this->getApp()->user) && $this->getApp()->user->loaded()) {
170+
if(isset($this->getApp()->user) && $this->getApp()->user->isLoaded()) {
171171
$data['updated_by'] = $this->getApp()->user->getId();
172172
}
173173
$data['updated_dts'] = new \DateTime();
@@ -238,10 +238,8 @@ Start by creating a class::
238238
}
239239
}
240240

241-
function softDelete($m) {
242-
if (!$m->loaded()) {
243-
throw (new \Atk4\Core\Exception('Model must be loaded before soft-deleting'))->addMoreInfo('model', $m);
244-
}
241+
function softDelete(Model $m) {
242+
$m->assertIsLoaded();
245243

246244
$id = $m->getId();
247245
if ($m->hook('beforeSoftDelete') === false) {
@@ -257,10 +255,8 @@ Start by creating a class::
257255
return $m;
258256
}
259257

260-
function restore($m) {
261-
if (!$m->loaded()) {
262-
throw (new \Atk4\Core\Exception(['Model must be loaded before restoring'))->addMoreInfo('model', $m);
263-
}
258+
function restore(Model $m) {
259+
$m->assertIsLoaded();
264260

265261
$id = $m->getId();
266262
if ($m->hook('beforeRestore') === false) {
@@ -348,9 +344,7 @@ before and just slightly modifying it::
348344
}
349345

350346
function softDelete(Model $m) {
351-
if (!$m->loaded()) {
352-
throw (new \Atk4\Core\Exception('Model must be loaded before soft-deleting'))->addMoreInfo('model', $m);
353-
}
347+
$m->assertIsLoaded();
354348

355349
$id = $m->getId();
356350

@@ -364,10 +358,8 @@ before and just slightly modifying it::
364358
$m->breakHook(false); // this will cancel original delete()
365359
}
366360

367-
function restore($m) {
368-
if (!$m->loaded()) {
369-
throw (new \Atk4\Core\Exception('Model must be loaded before restoring'))->addMoreInfo('model', $m);
370-
}
361+
function restore(Model $m) {
362+
$m->assertIsLoaded();
371363

372364
$id = $m->getId();
373365
if ($m->hook('beforeRestore') === false) {
@@ -436,7 +428,7 @@ inside your model are unique::
436428
$mm->addCondition($mm->id_field != $this->id);
437429
$mm = $mm->tryLoadBy($field, $m->get($field));
438430

439-
if ($mm->loaded()) {
431+
if ($mm->isLoaded()) {
440432
throw (new \Atk4\Core\Exception('Duplicate record exists'))
441433
->addMoreInfo('field', $field)
442434
->addMoreInfo('value', $m->get($field));
@@ -572,12 +564,12 @@ payment towards a most suitable invoice::
572564
// See if any invoices match by 'reference';
573565
$invoices = $invoices->tryLoadBy('reference', $this->get('reference'));
574566

575-
if (!$invoices->loaded()) {
567+
if (!$invoices->isLoaded()) {
576568

577569
// otherwise load any unpaid invoice
578570
$invoices = $invoices->tryLoadAny();
579571

580-
if(!$invoices->loaded()) {
572+
if(!$invoices->isLoaded()) {
581573

582574
// couldn't load any invoice.
583575
return;

docs/hooks.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Insert/Update Hooks
101101
-------------------
102102

103103
Insert/Update are triggered from inside save() method but are based on current
104-
state of :php:meth:`Model::loaded`:
104+
state of :php:meth:`Model::isLoaded`:
105105

106106
- beforeInsert($m, &$data) (creating new records only)
107107
- afterInsert($m, $id)

docs/persistence.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ Start by creating a beforeSave handler for Order::
483483
->addCondition('client_id', $this->get('client_id')) // same client
484484
->addCondition($this->id_field, '!=', $this->getId()) // has another order
485485
->tryLoadBy('ref', $this->get('ref')) // with same ref
486-
->loaded()
486+
->isLoaded()
487487
) {
488488
throw (new Exception('Order with ref already exists for this client'))
489489
->addMoreInfo('client', $this->get('client_id'))
@@ -581,7 +581,7 @@ application::
581581
// first, try to load it from MemCache
582582
$m = $this->mdb->add(clone $class)->tryLoad($id);
583583

584-
if (!$m->loaded()) {
584+
if (!$m->isLoaded()) {
585585

586586
// fall-back to load from SQL
587587
$m = $this->sql->add(clone $class)->load($id);
@@ -619,7 +619,7 @@ use a string). It will first be associated with the MemCache DB persistence and
619619
we will attempt to load a corresponding ID. Next, if no record is found in the
620620
cache::
621621

622-
if (!$m->loaded()) {
622+
if (!$m->isLoaded()) {
623623

624624
// fall-back to load from SQL
625625
$m = $this->sql->add(clone $class)->load($id);

docs/quickstart.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ You can load / unload records like this::
227227
$m->set('email', 'test@example.com');
228228
$m->save();
229229

230-
You can call `$m->loaded()` to see if there is active record and `$m->getId()` will
230+
You can call `$m->isLoaded()` to see if there is active record and `$m->getId()` will
231231
store the ID of active record. You can also un-load the record with `$m->unload()`.
232232

233233
By default no records are loaded and if you modify some field and attempt
@@ -453,7 +453,7 @@ Your Active Record was user john and after traversal you get a model with DataSe
453453
corresponding to all Systems that belong to user john. You can use the following
454454
to see number of records in DataSet or export DataSet::
455455

456-
$s->loaded();
456+
$s->isLoaded();
457457
$s->action('count')->getOne();
458458
$s->export();
459459
$s->action('count')->getDebugQuery();
@@ -470,7 +470,7 @@ This will create a Model_Client instance with a DataSet corresponding to all
470470
the Clients that are contained in all of the Systems that belong to user john.
471471
You can examine the this model further::
472472

473-
$c->loaded();
473+
$c->isLoaded();
474474
$c->action('count')->getOne();
475475
$c->export();
476476
$c->action('count')->getDebugQuery();
@@ -490,7 +490,7 @@ The third and final reference traversal type is "Active Record to Active Record"
490490
This results in an instance of Model_Country with Active Record set to the
491491
country of user john::
492492

493-
$cc->loaded();
493+
$cc->isLoaded();
494494
$cc->getId();
495495
$cc->get();
496496

docs/references.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ No condition will be applied by default so it's all up to you::
452452

453453
$m->addField('original_id', ['type' => 'integer']);
454454

455-
if ($m->loaded)) {
455+
if ($m->isLoaded())) {
456456
$archive->addCondition('original_id', $m->getId());
457457
// only show record of currently loaded record
458458
}

docs/sql.rst

+3-7
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ SQL Reference
106106
implements deep traversal::
107107

108108
$country_model = $customer_model->addCondition('is_vip', true)
109-
->ref('country_id'); // $model was not loaded!
109+
->ref('country_id'); // $model was not loaded!
110110

111111
.. php:method:: refLink
112112
@@ -364,9 +364,7 @@ the data::
364364
}
365365

366366
function getReportData($arg) {
367-
if (!$this->loaded()) {
368-
throw new Exception('Client must be loaded');
369-
}
367+
$this->assertIsLoaded();
370368

371369
return $this->expr("call get_client_report_data([client_id, arg])", [
372370
'arg' => $arg,
@@ -383,9 +381,7 @@ Here is another example using PHP generator::
383381
}
384382

385383
function fetchReportData($arg) {
386-
if (!$this->loaded()) {
387-
throw new Exception('Client must be loaded');
388-
}
384+
$this->assertIsLoaded();
389385

390386
foreach ($this->expr("call get_client_report_data([client_id, arg])", [
391387
'arg' => $arg,

src/Model.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -1227,13 +1227,30 @@ public function assertHasPersistence(string $methodName = null): void
12271227
}
12281228

12291229
/**
1230-
* Is model loaded?
1230+
* @deprecated will be removed in v4.0
12311231
*/
12321232
public function loaded(): bool
1233+
{
1234+
'trigger_error'('Method is deprecated. Use isLoaded() instead', \E_USER_DEPRECATED);
1235+
1236+
return $this->isLoaded();
1237+
}
1238+
1239+
/**
1240+
* Is entity loaded?
1241+
*/
1242+
public function isLoaded(): bool
12331243
{
12341244
return $this->id_field && $this->getId() !== null && $this->_entityId !== null;
12351245
}
12361246

1247+
public function assertIsLoaded(): void
1248+
{
1249+
if (!$this->isLoaded()) {
1250+
throw new Exception('Expected loaded entity');
1251+
}
1252+
}
1253+
12371254
/**
12381255
* Unload model.
12391256
*
@@ -1280,7 +1297,7 @@ private function remapIdLoadToPersistence($id)
12801297
private function _loadThis(bool $isTryLoad, $id)
12811298
{
12821299
$this->assertIsEntity();
1283-
if ($this->loaded()) {
1300+
if ($this->isLoaded()) {
12841301
throw new Exception('Entity must be unloaded');
12851302
}
12861303

@@ -1567,7 +1584,7 @@ public function save(array $data = [])
15671584
if (($errors = $this->validate('save')) !== []) {
15681585
throw new ValidationException($errors, $this);
15691586
}
1570-
$is_update = $this->loaded();
1587+
$is_update = $this->isLoaded();
15711588
if ($this->hook(self::HOOK_BEFORE_SAVE, [$is_update]) === false) {
15721589
return $this;
15731590
}
@@ -1643,7 +1660,7 @@ public function save(array $data = [])
16431660
}
16441661
}
16451662

1646-
if ($this->loaded()) {
1663+
if ($this->isLoaded()) {
16471664
$dirtyRef = $this->dirtyAfterReload;
16481665
}
16491666

@@ -1888,12 +1905,10 @@ public function delete($id = null)
18881905
return $this;
18891906
}
18901907

1891-
$this->assertIsEntity();
1908+
$this->assertIsLoaded();
18921909

18931910
if ($this->read_only) {
18941911
throw new Exception('Model is read-only and cannot be deleted');
1895-
} elseif (!$this->loaded()) {
1896-
throw new Exception('No active record is set, unable to delete.');
18971912
}
18981913

18991914
$this->atomic(function () {

src/Model/UserAction.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ protected function validateBeforeExecute(): void
141141
// Verify some records scope cases
142142
switch ($this->appliesTo) {
143143
case self::APPLIES_TO_NO_RECORDS:
144-
if ($this->getEntity()->loaded()) {
144+
if ($this->getEntity()->isLoaded()) {
145145
throw (new Exception('This user action can be executed on non-existing record only.'))
146146
->addMoreInfo('id', $this->getEntity()->getId());
147147
}
148148

149149
break;
150150
case self::APPLIES_TO_SINGLE_RECORD:
151-
if (!$this->getEntity()->loaded()) {
151+
if (!$this->getEntity()->isLoaded()) {
152152
throw new Exception('This user action requires you to load existing record first.');
153153
}
154154

src/Persistence/Sql.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ public function action(Model $model, string $type, array $args = [])
456456
$this->initQueryConditions($model, $query);
457457
$this->setLimitOrder($model, $query);
458458

459-
if ($model->isEntity() && $model->loaded()) {
459+
if ($model->isEntity() && $model->isLoaded()) {
460460
$query->where($model->getField($model->id_field), $model->getId());
461461
}
462462

src/Reference/HasMany.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function getOurFieldValueForRefCondition(Model $ourModel)
3838
{
3939
$ourModel = $this->getOurModel($ourModel);
4040

41-
if ($ourModel->isEntity() && $ourModel->loaded()) {
41+
if ($ourModel->isEntity() && $ourModel->isLoaded()) {
4242
return $this->our_field
4343
? $ourModel->get($this->our_field)
4444
: $ourModel->getId();

src/Reference/HasOneSql.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function ref(Model $ourModel, array $defaults = []): Model
122122
// if our_field is the id_field and is being used in the reference
123123
// we should persist the relation in condtition
124124
// example - $model->load(1)->ref('refLink')->import($rows);
125-
if ($ourModel->isEntity() && $ourModel->loaded() && !$theirModel->loaded()) {
125+
if ($ourModel->isEntity() && $ourModel->isLoaded() && !$theirModel->isLoaded()) {
126126
if ($ourModel->id_field === $this->getOurFieldName()) {
127127
return $theirModel->getModel()
128128
->addCondition($theirFieldName, $this->getOurFieldValue($ourModel));

tests/ConditionSqlTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ public function testExpressionJoin(): void
263263
$mm = clone $m;
264264
$mm->addCondition($mm->expr('[name] = [surname]'));
265265
$mm2 = $mm->tryLoad(1);
266-
$this->assertFalse($mm2->loaded());
266+
$this->assertFalse($mm2->isLoaded());
267267
$mm2 = $mm->tryLoad(2);
268268
$this->assertSame('Sue', $mm2->get('name'));
269269
$this->assertSame('+321 sues', $mm2->get('contact_phone'));
270270
$mm2 = $mm->tryLoad(3);
271-
$this->assertFalse($mm2->loaded());
271+
$this->assertFalse($mm2->isLoaded());
272272

273273
$mm = clone $m;
274274
$mm->addCondition($mm->expr('\'+123 smiths\' = [contact_phone]'));

tests/ContainsOneTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function testContainsOne(): void
8383

8484
// check do we have address set
8585
$a = $i->addr;
86-
$this->assertFalse($a->loaded());
86+
$this->assertFalse($a->isLoaded());
8787

8888
// now store some address
8989
$a->setMulti($row = [
@@ -153,12 +153,12 @@ public function testContainsOne(): void
153153
// so far so good. now let's try to delete door_code
154154
$i->addr->door_code->delete();
155155
$this->assertNull($i->addr->get($i->addr->fieldName()->door_code));
156-
$this->assertFalse($i->addr->door_code->loaded());
156+
$this->assertFalse($i->addr->door_code->isLoaded());
157157

158158
// and now delete address
159159
$i->addr->delete();
160160
$this->assertNull($i->get($i->fieldName()->addr));
161-
$this->assertFalse($i->addr->loaded());
161+
$this->assertFalse($i->addr->isLoaded());
162162

163163
//var_dump($i->export(), $i->export(null, null, false));
164164
}

0 commit comments

Comments
 (0)