-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathPivotedPromise.php
More file actions
161 lines (132 loc) · 4.16 KB
/
Copy pathPivotedPromise.php
File metadata and controls
161 lines (132 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
declare(strict_types=1);
namespace Cycle\ORM\Relation\Pivoted;
use Cycle\ORM\Exception\ORMException;
use Cycle\ORM\Heap\Node;
use Cycle\ORM\Iterator;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Parser\RootNode;
use Cycle\ORM\Promise\PromiseInterface;
use Cycle\ORM\Relation;
use Cycle\ORM\Select;
use Cycle\ORM\Select\JoinableLoader;
use Cycle\ORM\Select\Loader\ManyToManyLoader;
/**
* Promise use loader to configure query and it's scope.
*/
final class PivotedPromise implements PromiseInterface
{
/** @var ORMInterface @internal */
private $orm;
/** @var string */
private $target;
/** @var array @internal */
private $relationSchema = [];
/** @var mixed */
private $innerKey;
/** @var null|PivotedStorage */
private $resolved;
/**
* @param ORMInterface $orm
* @param string $target
* @param array $relationSchema
* @param mixed $innerKey
*/
public function __construct(ORMInterface $orm, string $target, array $relationSchema, $innerKey)
{
$this->orm = $orm;
$this->target = $target;
$this->relationSchema = $relationSchema;
$this->innerKey = $innerKey;
}
/**
* @inheritdoc
*/
public function __loaded(): bool
{
return $this->orm === null;
}
/**
* @inheritdoc
*/
public function __role(): string
{
return $this->target;
}
/**
* @inheritdoc
*/
public function __scope(): array
{
return [
$this->relationSchema[Relation::INNER_KEY] => $this->innerKey
];
}
/**
* @return PivotedStorage
*/
public function __resolve()
{
/*
* This method emulates the selection of MtM nodes by skipping parent relation (as it usually done
* in query) and injecting parent ID instead.
*/
if ($this->orm === null) {
return $this->resolved;
}
if (!$this->orm instanceof Select\SourceProviderInterface) {
throw new ORMException('PivotedPromise require ORM to implement SourceFactoryInterface');
}
// getting scoped query
$query = (new Select\RootLoader($this->orm, $this->target))->buildQuery();
// responsible for all the scoping
$loader = new ManyToManyLoader(
$this->orm,
$this->orm->getSource($this->target)->getTable(),
$this->target,
$this->relationSchema
);
/** @var ManyToManyLoader $loader */
$loader = $loader->withContext($loader, [
'as' => $this->target,
'method' => JoinableLoader::POSTLOAD
]);
$query = $loader->configureQuery($query, [$this->innerKey]);
// we are going to add pivot node into virtual root node (only ID) to aggregate the results
$root = new RootNode(
[$this->relationSchema[Relation::INNER_KEY]],
$this->relationSchema[Relation::INNER_KEY]
);
$node = $loader->createNode();
$root->linkNode('output', $node);
// emulate presence of parent entity
$root->parseRow(0, [$this->innerKey]);
$iterator = $query->getIterator();
foreach ($iterator as $row) {
$node->parseRow(0, $row);
}
$iterator->close();
// load all eager relations, forbid loader to re-fetch data (make it think it was joined)
$loader->withContext($loader, ['method' => JoinableLoader::INLOAD])->loadData($node);
$elements = [];
$pivotData = new \SplObjectStorage();
$iterator = new Iterator($this->orm, $this->target, $root->getResult()[0]['output'], true);
foreach ($iterator as $pivot => $entity) {
$pivotData[$entity] = $this->orm->make(
$this->relationSchema[Relation::THROUGH_ENTITY],
$pivot,
Node::MANAGED
);
$elements[] = $entity;
}
$this->resolved = new PivotedStorage($elements, $pivotData);
$this->orm = null;
return $this->resolved;
}
}