@@ -622,21 +622,18 @@ persistence layer to load or save anything. Next I need a beforeSave handler:
622
622
```
623
623
$this->onHookShort(Model::HOOK_BEFORE_SAVE, function () {
624
624
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());
628
627
}
629
628
630
629
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());
634
632
}
635
633
636
634
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());
640
637
}
641
638
});
642
639
```
@@ -647,43 +644,6 @@ differently from PHP's default behavior. See documentation for Model::isset
647
644
This technique allows you to hide the complexity of the lookups and also embed
648
645
the necessary queries inside your "insert" query.
649
646
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
-
687
647
## Inserting Hierarchical Data
688
648
689
649
In this example I'll be building API that allows me to insert multi-model
0 commit comments