@@ 8-26 (lines=19) @@ | ||
5 | /** |
|
6 | * Foreign-key reference resolution. |
|
7 | */ |
|
8 | trait ReferenceResolverTrait |
|
9 | { |
|
10 | /** |
|
11 | * Retrieve a reference by foreign key. |
|
12 | * |
|
13 | * @param RepositoryInterface $repository Repository to query. |
|
14 | * @param string $attr Name of foreign key attribute. |
|
15 | * @param string $key Name of primary key attribute in referenced table. |
|
16 | * |
|
17 | * @return mixed Model instance if found, null otherwise. |
|
18 | */ |
|
19 | public function getReference($repository, $attr, $key = 'id') |
|
20 | { |
|
21 | if (isset($this->$attr)) { |
|
22 | return ($repository->find($key, $this->$attr) ?: null); |
|
23 | } |
|
24 | return null; |
|
25 | } |
|
26 | } |
|
27 |
@@ 8-29 (lines=22) @@ | ||
5 | /** |
|
6 | * Soft-deletion-aware foreign-key reference resolution. |
|
7 | */ |
|
8 | trait SoftReferenceResolverTrait |
|
9 | { |
|
10 | use ReferenceResolverTrait; |
|
11 | ||
12 | ||
13 | /** |
|
14 | * Retrieve a reference by foreign key, ignoring soft-deleted entries. |
|
15 | * |
|
16 | * @param SoftRepositoryInterface $repository Repository to query. |
|
17 | * @param string $attr Name of foreign key attribute. |
|
18 | * @param string $key Name of primary key attribute in referenced table. |
|
19 | * |
|
20 | * @return mixed Model instance if found, null otherwise. |
|
21 | */ |
|
22 | public function getReferenceSoft($repository, $attr, $key = 'id') |
|
23 | { |
|
24 | if (isset($this->$attr)) { |
|
25 | return ($repository->findSoft($key, $this->$attr) ?: null); |
|
26 | } |
|
27 | return null; |
|
28 | } |
|
29 | } |
|
30 |