|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Sirius\Orm\Relation; |
|
5
|
|
|
|
|
6
|
|
|
use Sirius\Orm\Entity\EntityInterface; |
|
7
|
|
|
use Sirius\Orm\Entity\LazyAggregate; |
|
8
|
|
|
use Sirius\Orm\Entity\Tracker; |
|
9
|
|
|
use Sirius\Orm\Mapper; |
|
10
|
|
|
use Sirius\Orm\Query; |
|
11
|
|
|
|
|
12
|
|
|
class Aggregate |
|
13
|
|
|
{ |
|
14
|
|
|
public function __construct(string $name, Relation $relation, array $options) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->name = $name; |
|
|
|
|
|
|
17
|
|
|
$this->relation = $relation; |
|
|
|
|
|
|
18
|
|
|
$this->options = $options; |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function getQuery(Tracker $tracker) |
|
22
|
|
|
{ |
|
23
|
|
|
$keys = $this->relation->getKeyPairs(); |
|
24
|
|
|
|
|
25
|
|
|
/** @var Query $query */ |
|
26
|
|
|
$query = $this->relation->getQuery($tracker); |
|
27
|
|
|
$query->resetColumns(); |
|
28
|
|
|
$query->columns(...array_values($keys)); |
|
29
|
|
|
$query->columns(sprintf( |
|
30
|
|
|
'%s as %s', |
|
31
|
|
|
$this->options[RelationConfig::AGG_FUNCTION], |
|
32
|
|
|
$this->name |
|
33
|
|
|
)); |
|
34
|
|
|
|
|
35
|
|
|
$callback = $this->options[RelationConfig::AGG_CALLBACK] ?? null; |
|
36
|
|
|
if (is_callable($callback)) { |
|
37
|
|
|
$query = $callback($query); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$query->groupBy(...array_values($keys)); |
|
41
|
|
|
|
|
42
|
|
|
return $query; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function attachLazyAggregateToEntity(EntityInterface $entity, Tracker $tracker) |
|
46
|
|
|
{ |
|
47
|
|
|
$valueLoader = new LazyAggregate($entity, $tracker, $this); |
|
48
|
|
|
$this->relation->getNativeMapper()->setEntityAttribute($entity, $this->name, $valueLoader); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function attachAggregateToEntity(EntityInterface $entity, array $results) |
|
52
|
|
|
{ |
|
53
|
|
|
$keys = $this->relation->getKeyPairs(); |
|
|
|
|
|
|
54
|
|
|
$found = null; |
|
55
|
|
|
foreach ($results as $row) { |
|
56
|
|
|
if ($this->entityMatchesRow($entity, $row)) { |
|
57
|
|
|
$found = $row; |
|
58
|
|
|
break; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
$this->relation |
|
62
|
|
|
->getNativeMapper() |
|
63
|
|
|
->setEntityAttribute($entity, $this->name, $found ? $found[$this->name] : null); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function isLazyLoad() |
|
67
|
|
|
{ |
|
68
|
|
|
return !isset($this->options[RelationConfig::LOAD_STRATEGY]) || |
|
69
|
|
|
$this->options[RelationConfig::LOAD_STRATEGY] == RelationConfig::LOAD_LAZY; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function isEagerLoad() |
|
73
|
|
|
{ |
|
74
|
|
|
return isset($this->options[RelationConfig::LOAD_STRATEGY]) && |
|
75
|
|
|
$this->options[RelationConfig::LOAD_STRATEGY] == RelationConfig::LOAD_EAGER; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getName() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->name; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function entityMatchesRow(EntityInterface $entity, $row) |
|
84
|
|
|
{ |
|
85
|
|
|
$keys = $this->relation->getKeyPairs(); |
|
86
|
|
|
foreach ($keys as $nativeCol => $foreignCol) { |
|
87
|
|
|
$entityValue = $this->relation->getNativeMapper()->getEntityAttribute($entity, $nativeCol); |
|
88
|
|
|
$rowValue = $row[$foreignCol]; |
|
89
|
|
|
// if both native and foreign key values are present (not unlinked entities) they must be the same |
|
90
|
|
|
// otherwise we assume that the entities can be linked together |
|
91
|
|
|
if ($entityValue && $rowValue && $entityValue != $rowValue) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|