1 | <?php |
||
41 | class CollectionPersister |
||
42 | { |
||
43 | /** |
||
44 | * The DocumentManager instance. |
||
45 | * |
||
46 | * @var DocumentManager |
||
47 | */ |
||
48 | private $dm; |
||
49 | |||
50 | /** |
||
51 | * The PersistenceBuilder instance. |
||
52 | * |
||
53 | * @var PersistenceBuilder |
||
54 | */ |
||
55 | private $pb; |
||
56 | |||
57 | /** |
||
58 | * Constructs a new CollectionPersister instance. |
||
59 | * |
||
60 | * @param DocumentManager $dm |
||
61 | * @param PersistenceBuilder $pb |
||
62 | * @param UnitOfWork $uow |
||
63 | */ |
||
64 | 793 | public function __construct(DocumentManager $dm, PersistenceBuilder $pb, UnitOfWork $uow) |
|
70 | |||
71 | /** |
||
72 | * Deletes a PersistentCollection instance completely from a document using $unset. |
||
73 | * |
||
74 | * @param PersistentCollectionInterface $coll |
||
75 | * @param array $options |
||
76 | */ |
||
77 | 35 | public function delete(PersistentCollectionInterface $coll, array $options) |
|
90 | |||
91 | /** |
||
92 | * Updates a PersistentCollection instance deleting removed rows and |
||
93 | * inserting new rows. |
||
94 | * |
||
95 | * @param PersistentCollectionInterface $coll |
||
96 | * @param array $options |
||
97 | */ |
||
98 | 100 | public function update(PersistentCollectionInterface $coll, array $options) |
|
99 | { |
||
100 | 100 | $mapping = $coll->getMapping(); |
|
101 | |||
102 | 100 | if ($mapping['isInverseSide']) { |
|
103 | return; // ignore inverse side |
||
104 | } |
||
105 | |||
106 | 100 | switch ($mapping['strategy']) { |
|
107 | case ClassMetadataInfo::STORAGE_STRATEGY_ATOMIC_SET: |
||
108 | case ClassMetadataInfo::STORAGE_STRATEGY_ATOMIC_SET_ARRAY: |
||
109 | throw new \UnexpectedValueException($mapping['strategy'] . ' update collection strategy should have been handled by DocumentPersister. Please report a bug in issue tracker'); |
||
110 | |||
111 | case ClassMetadataInfo::STORAGE_STRATEGY_SET: |
||
112 | case ClassMetadataInfo::STORAGE_STRATEGY_SET_ARRAY: |
||
113 | 11 | $this->setCollection($coll, $options); |
|
114 | 11 | break; |
|
115 | |||
116 | case ClassMetadataInfo::STORAGE_STRATEGY_ADD_TO_SET: |
||
117 | case ClassMetadataInfo::STORAGE_STRATEGY_PUSH_ALL: |
||
118 | 90 | $coll->initialize(); |
|
119 | 90 | $this->deleteElements($coll, $options); |
|
120 | 90 | $this->insertElements($coll, $options); |
|
121 | 89 | break; |
|
122 | |||
123 | default: |
||
124 | throw new \UnexpectedValueException('Unsupported collection strategy: ' . $mapping['strategy']); |
||
125 | } |
||
126 | 99 | } |
|
127 | |||
128 | /** |
||
129 | * Sets a PersistentCollection instance. |
||
130 | * |
||
131 | * This method is intended to be used with the "set" or "setArray" |
||
132 | * strategies. The "setArray" strategy will ensure that the collection is |
||
133 | * set as a BSON array, which means the collection elements will be |
||
134 | * reindexed numerically before storage. |
||
135 | * |
||
136 | * @param PersistentCollectionInterface $coll |
||
137 | * @param array $options |
||
138 | */ |
||
139 | 11 | private function setCollection(PersistentCollectionInterface $coll, array $options) |
|
148 | |||
149 | /** |
||
150 | * Deletes removed elements from a PersistentCollection instance. |
||
151 | * |
||
152 | * This method is intended to be used with the "pushAll" and "addToSet" |
||
153 | * strategies. |
||
154 | * |
||
155 | * @param PersistentCollectionInterface $coll |
||
156 | * @param array $options |
||
157 | */ |
||
158 | 90 | private function deleteElements(PersistentCollectionInterface $coll, array $options) |
|
184 | |||
185 | /** |
||
186 | * Inserts new elements for a PersistentCollection instance. |
||
187 | * |
||
188 | * This method is intended to be used with the "pushAll" and "addToSet" |
||
189 | * strategies. |
||
190 | * |
||
191 | * @param PersistentCollectionInterface $coll |
||
192 | * @param array $options |
||
193 | */ |
||
194 | 90 | private function insertElements(PersistentCollectionInterface $coll, array $options) |
|
221 | |||
222 | /** |
||
223 | * Gets the parent information for a given PersistentCollection. It will |
||
224 | * retrieve the top-level persistent Document that the PersistentCollection |
||
225 | * lives in. We can use this to issue queries when updating a |
||
226 | * PersistentCollection that is multiple levels deep inside an embedded |
||
227 | * document. |
||
228 | * |
||
229 | * <code> |
||
230 | * list($path, $parent) = $this->getPathAndParent($coll) |
||
231 | * </code> |
||
232 | * |
||
233 | * @param PersistentCollectionInterface $coll |
||
234 | * @return array $pathAndParent |
||
235 | */ |
||
236 | 111 | private function getPathAndParent(PersistentCollectionInterface $coll) |
|
256 | |||
257 | /** |
||
258 | * Executes a query updating the given document. |
||
259 | * |
||
260 | * @param object $document |
||
261 | * @param array $newObj |
||
262 | * @param array $options |
||
263 | */ |
||
264 | 111 | private function executeQuery($document, array $newObj, array $options) |
|
279 | } |
||
280 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: