OneToManyPersister.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\ORM\Persisters;
  22. use Doctrine\ORM\PersistentCollection,
  23. Doctrine\ORM\UnitOfWork;
  24. /**
  25. * Persister for one-to-many collections.
  26. *
  27. * IMPORTANT:
  28. * This persister is only used for uni-directional one-to-many mappings on a foreign key
  29. * (which are not yet supported). So currently this persister is not used.
  30. *
  31. * @since 2.0
  32. * @author Roman Borschel <roman@code-factory.org>
  33. * @todo Remove
  34. */
  35. class OneToManyPersister extends AbstractCollectionPersister
  36. {
  37. /**
  38. * Generates the SQL UPDATE that updates a particular row's foreign
  39. * key to null.
  40. *
  41. * @param PersistentCollection $coll
  42. * @return string
  43. * @override
  44. */
  45. protected function _getDeleteRowSQL(PersistentCollection $coll)
  46. {
  47. $mapping = $coll->getMapping();
  48. $targetClass = $this->_em->getClassMetadata($mapping->getTargetEntityName());
  49. $table = $targetClass->getTableName();
  50. $ownerMapping = $targetClass->getAssociationMapping($mapping['mappedBy']);
  51. $setClause = '';
  52. foreach ($ownerMapping->sourceToTargetKeyColumns as $sourceCol => $targetCol) {
  53. if ($setClause != '') $setClause .= ', ';
  54. $setClause .= "$sourceCol = NULL";
  55. }
  56. $whereClause = '';
  57. foreach ($targetClass->getIdentifierColumnNames() as $idColumn) {
  58. if ($whereClause != '') $whereClause .= ' AND ';
  59. $whereClause .= "$idColumn = ?";
  60. }
  61. return array("UPDATE $table SET $setClause WHERE $whereClause", $this->_uow->getEntityIdentifier($element));
  62. }
  63. protected function _getInsertRowSQL(PersistentCollection $coll)
  64. {
  65. return "UPDATE xxx SET foreign_key = yyy WHERE foreign_key = zzz";
  66. }
  67. /* Not used for OneToManyPersister */
  68. protected function _getUpdateRowSQL(PersistentCollection $coll)
  69. {
  70. return;
  71. }
  72. /**
  73. * Generates the SQL UPDATE that updates all the foreign keys to null.
  74. *
  75. * @param PersistentCollection $coll
  76. */
  77. protected function _getDeleteSQL(PersistentCollection $coll)
  78. {
  79. }
  80. /**
  81. * Gets the SQL parameters for the corresponding SQL statement to delete
  82. * the given collection.
  83. *
  84. * @param PersistentCollection $coll
  85. */
  86. protected function _getDeleteSQLParameters(PersistentCollection $coll)
  87. {}
  88. /**
  89. * Gets the SQL parameters for the corresponding SQL statement to insert the given
  90. * element of the given collection into the database.
  91. *
  92. * @param PersistentCollection $coll
  93. * @param mixed $element
  94. */
  95. protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element)
  96. {}
  97. /**
  98. * Gets the SQL parameters for the corresponding SQL statement to delete the given
  99. * element from the given collection.
  100. *
  101. * @param PersistentCollection $coll
  102. * @param mixed $element
  103. */
  104. protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element)
  105. {}
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function count(PersistentCollection $coll)
  110. {
  111. $mapping = $coll->getMapping();
  112. $targetClass = $this->_em->getClassMetadata($mapping['targetEntity']);
  113. $sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']);
  114. $params = array();
  115. $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
  116. $where = '';
  117. foreach ($targetClass->associationMappings[$mapping['mappedBy']]['joinColumns'] AS $joinColumn) {
  118. if ($where != '') {
  119. $where .= ' AND ';
  120. }
  121. $where .= $joinColumn['name'] . " = ?";
  122. if ($targetClass->containsForeignIdentifier) {
  123. $params[] = $id[$sourceClass->getFieldForColumn($joinColumn['referencedColumnName'])];
  124. } else {
  125. $params[] = $id[$sourceClass->fieldNames[$joinColumn['referencedColumnName']]];
  126. }
  127. }
  128. $sql = "SELECT count(*) FROM " . $targetClass->getQuotedTableName($this->_conn->getDatabasePlatform()) . " WHERE " . $where;
  129. return $this->_conn->fetchColumn($sql, $params);
  130. }
  131. /**
  132. * @param PersistentCollection $coll
  133. * @param int $offset
  134. * @param int $length
  135. * @return \Doctrine\Common\Collections\ArrayCollection
  136. */
  137. public function slice(PersistentCollection $coll, $offset, $length = null)
  138. {
  139. $mapping = $coll->getMapping();
  140. return $this->_em->getUnitOfWork()
  141. ->getEntityPersister($mapping['targetEntity'])
  142. ->getOneToManyCollection($mapping, $coll->getOwner(), $offset, $length);
  143. }
  144. /**
  145. * @param PersistentCollection $coll
  146. * @param object $element
  147. */
  148. public function contains(PersistentCollection $coll, $element)
  149. {
  150. $mapping = $coll->getMapping();
  151. $uow = $this->_em->getUnitOfWork();
  152. // shortcut for new entities
  153. if ($uow->getEntityState($element, UnitOfWork::STATE_NEW) == UnitOfWork::STATE_NEW) {
  154. return false;
  155. }
  156. // only works with single id identifier entities. Will throw an exception in Entity Persisters
  157. // if that is not the case for the 'mappedBy' field.
  158. $id = current( $uow->getEntityIdentifier($coll->getOwner()) );
  159. return $uow->getEntityPersister($mapping['targetEntity'])
  160. ->exists($element, array($mapping['mappedBy'] => $id));
  161. }
  162. }