@@ -471,11 +471,13 @@ Next we need to define reference. Inside Model_Invoice add:
471
471
```
472
472
$this->hasMany('InvoicePayment', ['model' => [Model_InvoicePayment::class]]);
473
473
474
- $this->hasMany('Payment', ['model' => function (self $m ) {
475
- $p = new Model_Payment($m->getPersistence() );
474
+ $this->hasMany('Payment', ['model' => static function (Persistence $persistence ) {
475
+ $p = new Model_Payment($persistence );
476
476
$j = $p->join('invoice_payment.payment_id');
477
477
$j->addField('amount_closed');
478
478
$j->hasOne('invoice_id', ['model' => [Model_Invoice::class]]);
479
+
480
+ return $p;
479
481
}, 'theirField' => 'invoice_id']);
480
482
481
483
$this->onHookShort(Model::HOOK_BEFORE_DELETE, function () {
@@ -724,7 +726,7 @@ $this->hasOne('client_id', ['model' => [$this->client_class]]);
724
726
Alternatively you can replace model in the init() method of Model_Invoice:
725
727
726
728
```
727
- $this->getReference('client_id')->model = ' Model_Client' ;
729
+ $this->getReference('client_id')->model = [ Model_Client::class] ;
728
730
```
729
731
730
732
You can also use array here if you wish to pass additional information into
@@ -737,53 +739,25 @@ $this->getReference('client_id')->model = ['Model_Client', 'no_audit' => true];
737
739
Combined with our "Audit" handler above, this should allow you to relate
738
740
with deleted clients.
739
741
740
- The final use case is when some value inside the existing model should be
741
- passed into the related model. Let's say we have 'Model_Invoice' and we want to
742
- add 'payment_invoice_id' that points to 'Model_Payment'. However we want this
743
- field only to offer payments made by the same client. Inside Model_Invoice add:
744
-
745
- ```
746
- $this->hasOne('client_id', ['model' => [Model_Client::class]]);
747
-
748
- $this->hasOne('payment_invoice_id', ['model' => function (self $m) {
749
- return $m->ref('client_id')->ref('Payment');
750
- }]);
751
-
752
- /// how to use
753
-
754
- $m = new Model_Invoice($db);
755
- $m->set('client_id', 123);
756
-
757
- $m->set('payment_invoice_id', $m->ref('payment_invoice_id')->loadOne()->getId());
758
- ```
759
-
760
- In this case the payment_invoice_id will be set to ID of any payment by client
761
- 123 . There also may be some better uses:
762
-
763
- ```
764
- foreach ($cl->ref('Invoice') as $m) {
765
- $m->set('payment_invoice_id', $m->ref('payment_invoice_id')->loadOne()->getId());
766
- $m->save();
767
- }
768
- ```
769
-
770
742
## Narrowing Down Existing References
771
743
772
744
Agile Data allow you to define multiple references between same entities, but
773
745
sometimes that can be quite useful. Consider adding this inside your Model_Contact:
774
746
775
747
```
776
748
$this->hasMany('Invoice', ['model' => [Model_Invoice::class]]);
777
- $this->hasMany('OverdueInvoice', ['model' => function (self $m) {
778
- return $m->ref('Invoice')->addCondition('due', '<', date('Y-m-d'))
749
+
750
+ $this->hasMany('OverdueInvoice', ['model' => static function (Persistence $persistence) {
751
+ return (new Model_Invoice($persistence))
752
+ ->addCondition('due', '<', new \DateTime());
779
753
}]);
780
754
```
781
755
782
756
This way if you extend your class into 'Model_Client' and modify the 'Invoice'
783
757
reference to use different model:
784
758
785
759
```
786
- $this->getReference('Invoice')->model = ' Model_Invoice_Sale' ;
760
+ $this->getReference('Invoice')->model = [ Model_Invoice_Sale::class] ;
787
761
```
788
762
789
763
The 'OverdueInvoice' reference will be also properly adjusted.
0 commit comments