1 | <?php |
||
35 | class OneToManyPersister extends AbstractCollectionPersister |
||
36 | { |
||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public function delete(PersistentCollection $collection) |
||
41 | { |
||
42 | // The only valid case here is when you have weak entities. In this |
||
43 | // scenario, you have @OneToMany with orphanRemoval=true, and replacing |
||
44 | // the entire collection with a new would trigger this operation. |
||
45 | $mapping = $collection->getMapping(); |
||
46 | |||
47 | if ( ! $mapping['orphanRemoval']) { |
||
48 | // Handling non-orphan removal should never happen, as @OneToMany |
||
49 | // can only be inverse side. For owning side one to many, it is |
||
50 | // required to have a join table, which would classify as a ManyToManyPersister. |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | $targetClass = $this->em->getClassMetadata($mapping['targetEntity']); |
||
55 | |||
56 | return $targetClass->isInheritanceTypeJoined() |
||
57 | ? $this->deleteJoinedEntityCollection($collection) |
||
58 | : $this->deleteEntityCollection($collection); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function update(PersistentCollection $collection) |
||
65 | { |
||
66 | // This can never happen. One to many can only be inverse side. |
||
67 | // For owning side one to many, it is required to have a join table, |
||
68 | // then classifying it as a ManyToManyPersister. |
||
69 | return; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function get(PersistentCollection $collection, $index) |
||
76 | { |
||
77 | $mapping = $collection->getMapping(); |
||
78 | |||
79 | if ( ! isset($mapping['indexBy'])) { |
||
80 | throw new \BadMethodCallException("Selecting a collection by index is only supported on indexed collections."); |
||
81 | } |
||
82 | |||
83 | $persister = $this->uow->getEntityPersister($mapping['targetEntity']); |
||
84 | |||
85 | return $persister->load( |
||
86 | array( |
||
87 | $mapping['mappedBy'] => $collection->getOwner(), |
||
88 | $mapping['indexBy'] => $index |
||
89 | ), |
||
90 | null, |
||
91 | $mapping, |
||
92 | array(), |
||
93 | null, |
||
94 | 1 |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function count(PersistentCollection $collection) |
||
102 | { |
||
103 | $mapping = $collection->getMapping(); |
||
104 | $persister = $this->uow->getEntityPersister($mapping['targetEntity']); |
||
105 | |||
106 | // only works with single id identifier entities. Will throw an |
||
107 | // exception in Entity Persisters if that is not the case for the |
||
108 | // 'mappedBy' field. |
||
109 | $criteria = new Criteria(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner())); |
||
110 | |||
111 | return $persister->count($criteria); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function slice(PersistentCollection $collection, $offset, $length = null) |
||
118 | { |
||
119 | $mapping = $collection->getMapping(); |
||
120 | $persister = $this->uow->getEntityPersister($mapping['targetEntity']); |
||
121 | |||
122 | return $persister->getOneToManyCollection($mapping, $collection->getOwner(), $offset, $length); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function containsKey(PersistentCollection $collection, $key) |
||
129 | { |
||
130 | $mapping = $collection->getMapping(); |
||
131 | |||
132 | if ( ! isset($mapping['indexBy'])) { |
||
133 | throw new \BadMethodCallException("Selecting a collection by index is only supported on indexed collections."); |
||
134 | } |
||
135 | |||
136 | $persister = $this->uow->getEntityPersister($mapping['targetEntity']); |
||
137 | |||
138 | // only works with single id identifier entities. Will throw an |
||
139 | // exception in Entity Persisters if that is not the case for the |
||
140 | // 'mappedBy' field. |
||
141 | $criteria = new Criteria(); |
||
142 | |||
143 | $criteria->andWhere(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner())); |
||
144 | $criteria->andWhere(Criteria::expr()->eq($mapping['indexBy'], $key)); |
||
145 | |||
146 | return (bool) $persister->count($criteria); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function contains(PersistentCollection $collection, $element) |
||
153 | { |
||
154 | if ( ! $this->isValidEntityState($element)) { |
||
155 | return false; |
||
156 | } |
||
157 | |||
158 | $mapping = $collection->getMapping(); |
||
159 | $persister = $this->uow->getEntityPersister($mapping['targetEntity']); |
||
160 | |||
161 | // only works with single id identifier entities. Will throw an |
||
162 | // exception in Entity Persisters if that is not the case for the |
||
163 | // 'mappedBy' field. |
||
164 | $criteria = new Criteria(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner())); |
||
165 | |||
166 | return $persister->exists($element, $criteria); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function removeElement(PersistentCollection $collection, $element) |
||
173 | { |
||
174 | $mapping = $collection->getMapping(); |
||
175 | |||
176 | if ( ! $mapping['orphanRemoval']) { |
||
177 | // no-op: this is not the owning side, therefore no operations should be applied |
||
178 | return false; |
||
179 | } |
||
180 | |||
181 | if ( ! $this->isValidEntityState($element)) { |
||
182 | return false; |
||
183 | } |
||
184 | |||
185 | return $this |
||
186 | ->uow |
||
187 | ->getEntityPersister($mapping['targetEntity']) |
||
188 | ->delete($element); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function loadCriteria(PersistentCollection $collection, Criteria $criteria) |
||
198 | |||
199 | /** |
||
200 | * @param PersistentCollection $collection |
||
201 | * |
||
202 | * @return int |
||
203 | * |
||
204 | * @throws \Doctrine\DBAL\DBALException |
||
205 | */ |
||
206 | private function deleteEntityCollection(PersistentCollection $collection) |
||
225 | |||
226 | /** |
||
227 | * Delete Class Table Inheritance entities. |
||
228 | * A temporary table is needed to keep IDs to be deleted in both parent and child class' tables. |
||
229 | * |
||
230 | * Thanks Steve Ebersole (Hibernate) for idea on how to tackle reliably this scenario, we owe him a beer! =) |
||
231 | * |
||
232 | * @param PersistentCollection $collection |
||
233 | * |
||
234 | * @return int |
||
235 | * |
||
236 | * @throws \Doctrine\DBAL\DBALException |
||
237 | */ |
||
238 | private function deleteJoinedEntityCollection(PersistentCollection $collection) |
||
291 | } |
||
292 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: