Skip to content
This repository has been archived by the owner on Dec 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2787 from zsilbi/relation-params-callback
Browse files Browse the repository at this point in the history
Feature: callable relation params
  • Loading branch information
zsilbi authored Sep 28, 2020
2 parents 9b6d172 + accb208 commit 212e3f0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions en/db-models-relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ Depending on the needs of our application we might want to store data in one tab

Using relationships, you can get only those `Customers` that relate to our `Invoices` that have a certain `cst_status_flag`. Defining that constraint in the relationship allows you to let the model do all the work.

It also accepts a closure, which is evaluated every time before the related records are accessed. This enables the conditions to be automatically updated between queries.

```php
<?php

Expand Down Expand Up @@ -334,6 +336,27 @@ class Invoices extends Model
]
]
);

$container = $this->getDI();

$this->hasMany(
'inv_cst_id',
Customers::class,
'cst_id',
[
'reusable' => true,
'alias' => 'customersNearby',
'params' => function() use ($container) {
return [
'conditions' => 'cst_location = :location:',
'bind' => [
// Location can change between queries
'location' => $container->getShared('myLocationService')->myLocation,
]
];
}
]
);
}
}
```
Expand Down

0 comments on commit 212e3f0

Please sign in to comment.