Skip to content

Commit 0ae77bb

Browse files
authored
Drop Reference::refModel() and Model::refModel() methods (#1179)
1 parent 7ef966d commit 0ae77bb

10 files changed

+21
-109
lines changed

docs/advanced.md

+6-46
Original file line numberDiff line numberDiff line change
@@ -622,21 +622,18 @@ persistence layer to load or save anything. Next I need a beforeSave handler:
622622
```
623623
$this->onHookShort(Model::HOOK_BEFORE_SAVE, function () {
624624
if ($this->_isset('client_code') && !$this->_isset('client_id')) {
625-
$cl = $this->refModel('client_id');
626-
$cl->addCondition('code', $this->get('client_code'));
627-
$this->set('client_id', $cl->action('field', ['id']));
625+
$client = $this->ref('client_id');
626+
$this->set('client_id', $client->getId());
628627
}
629628
630629
if ($this->_isset('client_name') && !$this->_isset('client_id')) {
631-
$cl = $this->refModel('client_id');
632-
$cl->addCondition('name', 'like', $this->get('client_name'));
633-
$this->set('client_id', $cl->action('field', ['id']));
630+
$client = $this->ref('client_id');
631+
$this->set('client_id', $client->getId());
634632
}
635633
636634
if ($this->_isset('category') && !$this->_isset('category_id')) {
637-
$c = $this->refModel('category_id');
638-
$c->addCondition($c->titleField, 'like', $this->get('category'));
639-
$this->set('category_id', $c->action('field', ['id']));
635+
$category = $this->ref('category_id');
636+
$this->set('category_id', $category->getId());
640637
}
641638
});
642639
```
@@ -647,43 +644,6 @@ differently from PHP's default behavior. See documentation for Model::isset
647644
This technique allows you to hide the complexity of the lookups and also embed
648645
the necessary queries inside your "insert" query.
649646

650-
### Fallback to default value
651-
652-
You might wonder, with the lookup like that, how the default values will work?
653-
What if the user-specified entry is not found? Lets look at the code:
654-
655-
```
656-
if ($m->_isset('category') && !$m->_isset('category_id')) {
657-
$c = $this->refModel('category_id');
658-
$c->addCondition($c->titleField, 'like', $m->get('category'));
659-
$m->set('category_id', $c->action('field', ['id']));
660-
}
661-
```
662-
663-
So if category with a name is not found, then sub-query will return "NULL".
664-
If you wish to use a different value instead, you can create an expression:
665-
666-
```
667-
if ($m->_isset('category') && !$m->_isset('category_id')) {
668-
$c = $this->refModel('category_id');
669-
$c->addCondition($c->titleField, 'like', $m->get('category'));
670-
$m->set('category_id', $this->expr('coalesce([], [])', [
671-
$c->action('field', ['id']),
672-
$m->getField('category_id')->default,
673-
]));
674-
}
675-
```
676-
677-
The beautiful thing about this approach is that default can also be defined
678-
as a lookup query:
679-
680-
```
681-
$this->hasOne('category_id', 'Model_Category');
682-
$this->getField('category_id')->default =
683-
$this->refModel('category_id')->addCondition('name', 'Other')
684-
->action('field', ['id']);
685-
```
686-
687647
## Inserting Hierarchical Data
688648

689649
In this example I'll be building API that allows me to insert multi-model

docs/persistence.md

+2-10
Original file line numberDiff line numberDiff line change
@@ -746,10 +746,10 @@ executing:
746746
```
747747
$m = Model_Product($db);
748748
$m->addCondition('name', $productName);
749-
$action = $m->action('getOne', ['id']);
749+
$productIdAction = $m->action('field', ['id']);
750750
751751
$m = Model_Invoice($db);
752-
$m->insert(['qty' => 20, 'product_id' => $action]);
752+
$m->insert(['qty' => 20, 'product_id' => $productIdAction]);
753753
```
754754

755755
Insert operation will check if you are using same persistence.
@@ -759,14 +759,6 @@ result instead.
759759
Being able to embed actions inside next query allows Agile Data to reduce number
760760
of queries issued.
761761

762-
The default action type can be set when executing action, for example:
763-
764-
```
765-
$a = $m->action('field', 'user', 'getOne');
766-
767-
echo $a(); // same as $a->getOne();
768-
```
769-
770762
### SQL Actions
771763

772764
Currently only read-only actions are supported by `Persistence\Sql`:

docs/references.md

+4-19
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ If you are worried about performance you can keep 2 models in memory:
9595

9696
```
9797
$order = new Order($db);
98-
$client = $order->refModel('client_id');
98+
$client = $order->ref('client_id');
9999
100100
foreach ($order as $o) {
101101
$c = $client->load($o->get('client_id'));
@@ -267,7 +267,7 @@ Alternatively you may also specify either 'aggregate':
267267
```
268268
$book->hasMany('Pages', ['model' => [Page::class]])
269269
->addField('page_list', [
270-
'aggregate' => $book->refModel('Pages')->expr('group_concat([number], [])', ['-']),
270+
'aggregate' => $book->getReference('Pages')->createTheirModel()->expr('group_concat([number], [])', ['-']),
271271
]);
272272
```
273273

@@ -281,7 +281,7 @@ or 'field':
281281
as of 1.3.4 count's field defaults to `*` - no need to specify explicitly.
282282
:::
283283

284-
## hasMany / refLink / refModel
284+
## hasMany / refLink
285285

286286
:::{php:method} refLink($link)
287287
:::
@@ -315,13 +315,6 @@ from user
315315
where is_vip = 1
316316
```
317317

318-
:::{php:method} refModel($link)
319-
:::
320-
321-
There are many situations when you need to get referenced model instead of
322-
reference itself. In such case refModel() comes in as handy shortcut of doing
323-
`$model->refLink($link)->getModel()`.
324-
325318
## hasOne reference
326319

327320
:::{php:method} hasOne($link, ['model' => $model])
@@ -530,15 +523,7 @@ While {php:meth}`Model::ref()` returns a related model, {php:meth}`Model::getRef
530523
gives you the reference object itself so that you could perform some changes on it,
531524
such as import more fields with {php:meth}`Model::addField()`.
532525

533-
Or you can use {php:meth}`Model::refModel()` which will simply return referenced
534-
model and you can do fancy things with it.
535-
536-
```
537-
$refModel = $model->refModel('owner_id');
538-
```
539-
540-
You can also use {php:meth}`Model::hasReference()` to check if particular reference
541-
exists in model:
526+
{php:meth}`Model::hasReference()` can be used to check if particular reference exists in model:
542527

543528
```
544529
if ($model->hasReference('owner_id')) {

src/Model/ReferencesTrait.php

+2-14
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function getReferences(): array
147147
}
148148

149149
/**
150-
* Traverse to related model.
150+
* Traverse reference and create their model.
151151
*
152152
* @param array<string, mixed> $defaults
153153
*/
@@ -159,19 +159,7 @@ public function ref(string $link, array $defaults = []): Model
159159
}
160160

161161
/**
162-
* Return related model.
163-
*
164-
* @param array<string, mixed> $defaults
165-
*/
166-
public function refModel(string $link, array $defaults = []): Model
167-
{
168-
$reference = $this->getModel(true)->getReference($link);
169-
170-
return $reference->refModel($this, $defaults);
171-
}
172-
173-
/**
174-
* Returns model that can be used for generating sub-query actions.
162+
* Traverse reference and create their model but keep condition not materialized (for subquery actions).
175163
*
176164
* @param array<string, mixed> $defaults
177165
*/

src/Reference.php

+1-14
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ protected function getDefaultPersistence(Model $theirModel)
327327
}
328328

329329
/**
330-
* Returns referenced model without any extra conditions. However other
331-
* relationship types may override this to imply conditions.
330+
* Create their model. May be overridden to imply traversal conditions.
332331
*
333332
* @param array<string, mixed> $defaults
334333
*/
@@ -337,18 +336,6 @@ public function ref(Model $ourModel, array $defaults = []): Model
337336
return $this->createTheirModel($defaults);
338337
}
339338

340-
/**
341-
* Returns referenced model without any extra conditions. Ever when extended
342-
* must always respond with Model that does not look into current record
343-
* or scope.
344-
*
345-
* @param array<string, mixed> $defaults
346-
*/
347-
public function refModel(Model $ourModel, array $defaults = []): Model
348-
{
349-
return $this->createTheirModel($defaults);
350-
}
351-
352339
/**
353340
* @return array<string, mixed>
354341
*/

src/Reference/HasOneSql.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function addField(string $fieldName, string $theirFieldName = null, array
7979
$ourModel = $this->getOurModel(null);
8080

8181
// if caption/type is not defined in $defaults -> get it directly from the linked model field $theirFieldName
82-
$refModel = $ourModel->refModel($this->link);
82+
$refModel = $ourModel->getReference($this->link)->createTheirModel();
8383
$refModelField = $refModel->getField($theirFieldName);
8484
$defaults['type'] ??= $refModelField->type;
8585
$defaults['enum'] ??= $refModelField->enum;

src/Util/DeepCopy.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ protected function _copy(Model $source, Model $destination, array $references, a
250250
$this->debug('Proceeding with ' . $refKey);
251251

252252
// load destination model through $source
253-
$refSourceTable = $source->refModel($refKey)->table;
253+
$refSourceTable = $source->getModel()->getReference($refKey)->createTheirModel()->table;
254254

255255
if (isset($this->mapping[$refSourceTable])
256256
&& array_key_exists($source->get($refKey), $this->mapping[$refSourceTable])
@@ -273,7 +273,7 @@ protected function _copy(Model $source, Model $destination, array $references, a
273273
$refKey,
274274
$this->_copy(
275275
$source->ref($refKey),
276-
$destination->refModel($refKey),
276+
$destination->getModel()->getReference($refKey)->createTheirModel(),
277277
$refVal,
278278
$exclusions[$refKey] ?? [],
279279
$transforms[$refKey] ?? []

tests/ContainsManyTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testModelCaption(): void
6363

6464
// test caption of containsMany reference
6565
self::assertSame('My Invoice Lines', $i->getField($i->fieldName()->lines)->getCaption());
66-
self::assertSame('My Invoice Lines', $i->refModel($i->fieldName()->lines)->getModelCaption());
66+
self::assertSame('My Invoice Lines', $i->getReference($i->fieldName()->lines)->createTheirModel()->getModelCaption());
6767
self::assertSame('My Invoice Lines', $i->lines->getModelCaption());
6868
}
6969

tests/ContainsOneTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testModelCaption(): void
6262

6363
// test caption of containsOne reference
6464
self::assertSame('Secret Code', $a->getField($a->fieldName()->door_code)->getCaption());
65-
self::assertSame('Secret Code', $a->refModel($a->fieldName()->door_code)->getModelCaption());
65+
self::assertSame('Secret Code', $a->getReference($a->fieldName()->door_code)->createTheirModel()->getModelCaption());
6666
self::assertSame('Secret Code', $a->door_code->getModelCaption());
6767
}
6868

tests/ReferenceTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testModelCaption(): void
6565
$user->getModel()->hasMany('Orders', ['model' => $order, 'caption' => 'My Orders']);
6666

6767
// test caption of containsOne reference
68-
self::assertSame('My Orders', $user->refModel('Orders')->getModelCaption());
68+
self::assertSame('My Orders', $user->getModel()->getReference('Orders')->createTheirModel()->getModelCaption());
6969
self::assertSame('My Orders', $user->ref('Orders')->getModelCaption());
7070
}
7171

0 commit comments

Comments
 (0)