1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Relations; |
4
|
|
|
|
5
|
|
|
use Nip\Database\Query\AbstractQuery; |
6
|
|
|
use Nip\Records\AbstractModels\Record as Record; |
7
|
|
|
use Nip\Records\Collections\Collection as RecordCollection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class HasOneOrMany |
11
|
|
|
* @package Nip\Records\Relations |
12
|
|
|
*/ |
13
|
|
|
class HasOne extends Relation |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $type = 'hasOne'; |
19
|
|
|
|
20
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
1 |
|
public function generateFK() |
24
|
|
|
{ |
25
|
1 |
|
return $this->getManager()->getPrimaryFK(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritDoc |
30
|
|
|
*/ |
31
|
1 |
|
public function initResults() |
32
|
|
|
{ |
33
|
1 |
|
$withManager = $this->getWith(); |
34
|
1 |
|
$foreignKey = $this->getItem()->{$this->getPrimaryKey()}; |
35
|
1 |
|
$results = $withManager->findByField($this->getFK(), $foreignKey); |
36
|
1 |
|
if (count($results) > 0) { |
37
|
1 |
|
$this->setResults($results->rewind()); |
|
|
|
|
38
|
1 |
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this->setResults(false); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $dictionary |
46
|
|
|
* @param $collection |
47
|
|
|
* @param $record |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function getResultsFromCollectionDictionary($dictionary, $collection, $record) |
51
|
|
|
{ |
52
|
|
|
$primaryKey = $record->{$this->getPrimaryKey()}; |
53
|
|
|
if (isset($dictionary[$primaryKey])) { |
54
|
|
|
return $dictionary[$primaryKey]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Build model dictionary keyed by the relation's foreign key. |
62
|
|
|
* |
63
|
|
|
* @param RecordCollection $collection |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
protected function buildDictionary(RecordCollection $collection) |
67
|
|
|
{ |
68
|
|
|
if ($collection->isEmpty()) { |
69
|
|
|
return []; |
70
|
|
|
} |
71
|
|
|
$dictionary = []; |
72
|
|
|
foreach ($collection as $record) { |
73
|
|
|
$dictionary[$this->getDictionaryKey($record)] = $record; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $dictionary; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Record $record |
81
|
|
|
* @return array |
82
|
|
|
* @throws \Exception |
83
|
|
|
*/ |
84
|
|
|
protected function getDictionaryKey(Record $record) |
85
|
|
|
{ |
86
|
|
|
$withPK = $this->getPrimaryKey(); |
87
|
|
|
|
88
|
|
|
return $record->{$withPK}; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function populateQuerySpecific(AbstractQuery $query) |
95
|
|
|
{ |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.