|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Database\Query\AbstractQuery; |
|
6
|
|
|
use Nip\Records\AbstractModels\Record; |
|
7
|
|
|
use Nip\Records\Collections\Collection as RecordCollection; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class BelongsTo |
|
11
|
|
|
* @package Nip\Records\Relations |
|
12
|
|
|
*/ |
|
13
|
|
|
class BelongsTo extends Relation |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $type = 'belongsTo'; |
|
20
|
|
|
|
|
21
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
2 |
|
public function generateFK() |
|
25
|
|
|
{ |
|
26
|
2 |
|
return $this->getWith()->getPrimaryFK(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
public function initResults() |
|
30
|
|
|
{ |
|
31
|
2 |
|
$manager = $this->getWith(); |
|
32
|
2 |
|
$foreignKey = $this->getItem()->{$this->getFK()}; |
|
33
|
2 |
|
$this->setResults($manager->findOne($foreignKey)); |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param $dictionary |
|
38
|
|
|
* @param $collection |
|
39
|
|
|
* @param $record |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getResultsFromCollectionDictionary($dictionary, $collection, $record) |
|
43
|
|
|
{ |
|
44
|
|
|
$primaryKey = $record->{$this->getFK()}; |
|
45
|
|
|
if (isset($dictionary[$primaryKey])) { |
|
46
|
|
|
return $dictionary[$primaryKey]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Build model dictionary keyed by the relation's foreign key. |
|
54
|
|
|
* |
|
55
|
|
|
* @param RecordCollection $collection |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
View Code Duplication |
protected function buildDictionary(RecordCollection $collection) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
if ($collection->isEmpty()) { |
|
61
|
|
|
return []; |
|
62
|
|
|
} |
|
63
|
|
|
$dictionary = []; |
|
64
|
|
|
foreach ($collection as $record) { |
|
65
|
|
|
$dictionary[$this->getDictionaryKey($record)] = $record; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $dictionary; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Record $record |
|
73
|
|
|
* @return array |
|
74
|
|
|
* @throws \Exception |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getDictionaryKey(Record $record) |
|
77
|
|
|
{ |
|
78
|
|
|
$withPK = $this->getWithPK(); |
|
79
|
|
|
|
|
80
|
|
|
return $record->{$withPK}; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritdoc |
|
85
|
|
|
*/ |
|
86
|
|
|
public function populateQuerySpecific(AbstractQuery $query) |
|
87
|
|
|
{ |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.