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 | 722 | public function __construct(DocumentManager $dm, PersistenceBuilder $pb, UnitOfWork $uow) |
|
65 | { |
||
66 | 722 | $this->dm = $dm; |
|
67 | 722 | $this->pb = $pb; |
|
68 | 722 | $this->uow = $uow; |
|
|
|||
69 | 722 | } |
|
70 | |||
71 | /** |
||
72 | * Deletes a PersistentCollection instance completely from a document using $unset. |
||
73 | * |
||
74 | * @param PersistentCollectionInterface $coll |
||
75 | * @param array $options |
||
76 | */ |
||
77 | 34 | public function delete(PersistentCollectionInterface $coll, array $options) |
|
78 | { |
||
79 | 34 | $mapping = $coll->getMapping(); |
|
80 | 34 | if ($mapping['isInverseSide']) { |
|
81 | return; // ignore inverse side |
||
82 | } |
||
83 | 34 | if (CollectionHelper::isAtomic($mapping['strategy'])) { |
|
84 | throw new \UnexpectedValueException($mapping['strategy'] . ' delete collection strategy should have been handled by DocumentPersister. Please report a bug in issue tracker'); |
||
85 | } |
||
86 | 34 | list($propertyPath, $parent) = $this->getPathAndParent($coll); |
|
87 | 34 | $query = array('$unset' => array($propertyPath => true)); |
|
88 | 34 | $this->executeQuery($parent, $query, $options); |
|
89 | 33 | } |
|
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 | 97 | public function update(PersistentCollectionInterface $coll, array $options) |
|
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 | 87 | 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 | 87 | 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 | 107 | 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 | 107 | 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: