OneToManyPersister.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Persisters;
  20. use Doctrine\ORM\PersistentCollection;
  21. use Doctrine\ORM\UnitOfWork;
  22. /**
  23. * Persister for one-to-many collections.
  24. *
  25. * @author Roman Borschel <roman@code-factory.org>
  26. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  27. * @author Alexander <iam.asm89@gmail.com>
  28. * @since 2.0
  29. */
  30. class OneToManyPersister extends AbstractCollectionPersister
  31. {
  32. /**
  33. * {@inheritdoc}
  34. *
  35. * @override
  36. */
  37. public function get(PersistentCollection $coll, $index)
  38. {
  39. $mapping = $coll->getMapping();
  40. $uow = $this->em->getUnitOfWork();
  41. $persister = $uow->getEntityPersister($mapping['targetEntity']);
  42. if (!isset($mapping['indexBy'])) {
  43. throw new \BadMethodCallException("Selecting a collection by index is only supported on indexed collections.");
  44. }
  45. return $persister->load(array($mapping['mappedBy'] => $coll->getOwner(), $mapping['indexBy'] => $index), null, null, array(), 0, 1);
  46. }
  47. /**
  48. * Generates the SQL UPDATE that updates a particular row's foreign
  49. * key to null.
  50. *
  51. * @param \Doctrine\ORM\PersistentCollection $coll
  52. *
  53. * @return string
  54. *
  55. * @override
  56. */
  57. protected function getDeleteRowSQL(PersistentCollection $coll)
  58. {
  59. $mapping = $coll->getMapping();
  60. $class = $this->em->getClassMetadata($mapping['targetEntity']);
  61. $tableName = $this->quoteStrategy->getTableName($class, $this->platform);
  62. $idColumns = $class->getIdentifierColumnNames();
  63. return 'DELETE FROM ' . $tableName
  64. . ' WHERE ' . implode('= ? AND ', $idColumns) . ' = ?';
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element)
  70. {
  71. return array_values($this->uow->getEntityIdentifier($element));
  72. }
  73. /**
  74. * {@inheritdoc}
  75. *
  76. * @throws \BadMethodCallException Not used for OneToManyPersister.
  77. */
  78. protected function getInsertRowSQL(PersistentCollection $coll)
  79. {
  80. throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister");
  81. }
  82. /**
  83. * {@inheritdoc}
  84. *
  85. * @throws \BadMethodCallException Not used for OneToManyPersister.
  86. */
  87. protected function getInsertRowSQLParameters(PersistentCollection $coll, $element)
  88. {
  89. throw new \BadMethodCallException("Insert Row SQL is not used for OneToManyPersister");
  90. }
  91. /**
  92. * {@inheritdoc}
  93. *
  94. * @throws \BadMethodCallException Not used for OneToManyPersister.
  95. */
  96. protected function getUpdateRowSQL(PersistentCollection $coll)
  97. {
  98. throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
  99. }
  100. /**
  101. * {@inheritdoc}
  102. *
  103. * @throws \BadMethodCallException Not used for OneToManyPersister.
  104. */
  105. protected function getDeleteSQL(PersistentCollection $coll)
  106. {
  107. throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
  108. }
  109. /**
  110. * {@inheritdoc}
  111. *
  112. * @throws \BadMethodCallException Not used for OneToManyPersister.
  113. */
  114. protected function getDeleteSQLParameters(PersistentCollection $coll)
  115. {
  116. throw new \BadMethodCallException("Update Row SQL is not used for OneToManyPersister");
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function count(PersistentCollection $coll)
  122. {
  123. $mapping = $coll->getMapping();
  124. $targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
  125. $sourceClass = $this->em->getClassMetadata($mapping['sourceEntity']);
  126. $id = $this->em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
  127. $whereClauses = array();
  128. $params = array();
  129. $joinColumns = $targetClass->associationMappings[$mapping['mappedBy']]['joinColumns'];
  130. foreach ($joinColumns as $joinColumn) {
  131. $whereClauses[] = $joinColumn['name'] . ' = ?';
  132. $params[] = ($targetClass->containsForeignIdentifier)
  133. ? $id[$sourceClass->getFieldForColumn($joinColumn['referencedColumnName'])]
  134. : $id[$sourceClass->fieldNames[$joinColumn['referencedColumnName']]];
  135. }
  136. $filterTargetClass = $this->em->getClassMetadata($targetClass->rootEntityName);
  137. foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
  138. if ($filterExpr = $filter->addFilterConstraint($filterTargetClass, 't')) {
  139. $whereClauses[] = '(' . $filterExpr . ')';
  140. }
  141. }
  142. $sql = 'SELECT count(*)'
  143. . ' FROM ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' t'
  144. . ' WHERE ' . implode(' AND ', $whereClauses);
  145. return $this->conn->fetchColumn($sql, $params);
  146. }
  147. /**
  148. * @param \Doctrine\ORM\PersistentCollection $coll
  149. * @param int $offset
  150. * @param int|null $length
  151. *
  152. * @return \Doctrine\Common\Collections\ArrayCollection
  153. */
  154. public function slice(PersistentCollection $coll, $offset, $length = null)
  155. {
  156. $mapping = $coll->getMapping();
  157. $uow = $this->em->getUnitOfWork();
  158. $persister = $uow->getEntityPersister($mapping['targetEntity']);
  159. return $persister->getOneToManyCollection($mapping, $coll->getOwner(), $offset, $length);
  160. }
  161. /**
  162. * @param \Doctrine\ORM\PersistentCollection $coll
  163. * @param object $element
  164. *
  165. * @return boolean
  166. */
  167. public function contains(PersistentCollection $coll, $element)
  168. {
  169. $mapping = $coll->getMapping();
  170. $uow = $this->em->getUnitOfWork();
  171. // shortcut for new entities
  172. $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
  173. if ($entityState === UnitOfWork::STATE_NEW) {
  174. return false;
  175. }
  176. // Entity is scheduled for inclusion
  177. if ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element)) {
  178. return false;
  179. }
  180. $persister = $uow->getEntityPersister($mapping['targetEntity']);
  181. // only works with single id identifier entities. Will throw an
  182. // exception in Entity Persisters if that is not the case for the
  183. // 'mappedBy' field.
  184. $id = current( $uow->getEntityIdentifier($coll->getOwner()));
  185. return $persister->exists($element, array($mapping['mappedBy'] => $id));
  186. }
  187. /**
  188. * @param \Doctrine\ORM\PersistentCollection $coll
  189. * @param object $element
  190. *
  191. * @return boolean
  192. */
  193. public function removeElement(PersistentCollection $coll, $element)
  194. {
  195. $mapping = $coll->getMapping();
  196. if ( ! $mapping['orphanRemoval']) {
  197. // no-op: this is not the owning side, therefore no operations should be applied
  198. return false;
  199. }
  200. $uow = $this->em->getUnitOfWork();
  201. // shortcut for new entities
  202. $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
  203. if ($entityState === UnitOfWork::STATE_NEW) {
  204. return false;
  205. }
  206. // If Entity is scheduled for inclusion, it is not in this collection.
  207. // We can assure that because it would have return true before on array check
  208. if ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element)) {
  209. return false;
  210. }
  211. $this
  212. ->uow
  213. ->getEntityPersister($mapping['targetEntity'])
  214. ->delete($element);
  215. return true;
  216. }
  217. }