AbstractCollectionPersister.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Persisters;
  20. use Doctrine\ORM\EntityManager,
  21. Doctrine\ORM\PersistentCollection;
  22. /**
  23. * Base class for all collection persisters.
  24. *
  25. * @since 2.0
  26. * @author Roman Borschel <roman@code-factory.org>
  27. */
  28. abstract class AbstractCollectionPersister
  29. {
  30. /**
  31. * @var EntityManager
  32. */
  33. protected $_em;
  34. /**
  35. * @var \Doctrine\DBAL\Connection
  36. */
  37. protected $_conn;
  38. /**
  39. * @var \Doctrine\ORM\UnitOfWork
  40. */
  41. protected $_uow;
  42. /**
  43. * Initializes a new instance of a class derived from AbstractCollectionPersister.
  44. *
  45. * @param \Doctrine\ORM\EntityManager $em
  46. */
  47. public function __construct(EntityManager $em)
  48. {
  49. $this->_em = $em;
  50. $this->_uow = $em->getUnitOfWork();
  51. $this->_conn = $em->getConnection();
  52. }
  53. /**
  54. * Deletes the persistent state represented by the given collection.
  55. *
  56. * @param PersistentCollection $coll
  57. */
  58. public function delete(PersistentCollection $coll)
  59. {
  60. $mapping = $coll->getMapping();
  61. if ( ! $mapping['isOwningSide']) {
  62. return; // ignore inverse side
  63. }
  64. $sql = $this->_getDeleteSQL($coll);
  65. $this->_conn->executeUpdate($sql, $this->_getDeleteSQLParameters($coll));
  66. }
  67. /**
  68. * Gets the SQL statement for deleting the given collection.
  69. *
  70. * @param PersistentCollection $coll
  71. */
  72. abstract protected function _getDeleteSQL(PersistentCollection $coll);
  73. /**
  74. * Gets the SQL parameters for the corresponding SQL statement to delete
  75. * the given collection.
  76. *
  77. * @param PersistentCollection $coll
  78. */
  79. abstract protected function _getDeleteSQLParameters(PersistentCollection $coll);
  80. /**
  81. * Updates the given collection, synchronizing it's state with the database
  82. * by inserting, updating and deleting individual elements.
  83. *
  84. * @param PersistentCollection $coll
  85. */
  86. public function update(PersistentCollection $coll)
  87. {
  88. $mapping = $coll->getMapping();
  89. if ( ! $mapping['isOwningSide']) {
  90. return; // ignore inverse side
  91. }
  92. $this->deleteRows($coll);
  93. //$this->updateRows($coll);
  94. $this->insertRows($coll);
  95. }
  96. public function deleteRows(PersistentCollection $coll)
  97. {
  98. $deleteDiff = $coll->getDeleteDiff();
  99. $sql = $this->_getDeleteRowSQL($coll);
  100. foreach ($deleteDiff as $element) {
  101. $this->_conn->executeUpdate($sql, $this->_getDeleteRowSQLParameters($coll, $element));
  102. }
  103. }
  104. //public function updateRows(PersistentCollection $coll)
  105. //{}
  106. public function insertRows(PersistentCollection $coll)
  107. {
  108. $insertDiff = $coll->getInsertDiff();
  109. $sql = $this->_getInsertRowSQL($coll);
  110. foreach ($insertDiff as $element) {
  111. $this->_conn->executeUpdate($sql, $this->_getInsertRowSQLParameters($coll, $element));
  112. }
  113. }
  114. public function count(PersistentCollection $coll)
  115. {
  116. throw new \BadMethodCallException("Counting the size of this persistent collection is not supported by this CollectionPersister.");
  117. }
  118. public function slice(PersistentCollection $coll, $offset, $length = null)
  119. {
  120. throw new \BadMethodCallException("Slicing elements is not supported by this CollectionPersister.");
  121. }
  122. public function contains(PersistentCollection $coll, $element)
  123. {
  124. throw new \BadMethodCallException("Checking for existance of an element is not supported by this CollectionPersister.");
  125. }
  126. public function containsKey(PersistentCollection $coll, $key)
  127. {
  128. throw new \BadMethodCallException("Checking for existance of a key is not supported by this CollectionPersister.");
  129. }
  130. public function get(PersistentCollection $coll, $index)
  131. {
  132. throw new \BadMethodCallException("Selecting a collection by index is not supported by this CollectionPersister.");
  133. }
  134. /**
  135. * Gets the SQL statement used for deleting a row from the collection.
  136. *
  137. * @param PersistentCollection $coll
  138. */
  139. abstract protected function _getDeleteRowSQL(PersistentCollection $coll);
  140. /**
  141. * Gets the SQL parameters for the corresponding SQL statement to delete the given
  142. * element from the given collection.
  143. *
  144. * @param PersistentCollection $coll
  145. * @param mixed $element
  146. */
  147. abstract protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element);
  148. /**
  149. * Gets the SQL statement used for updating a row in the collection.
  150. *
  151. * @param PersistentCollection $coll
  152. */
  153. abstract protected function _getUpdateRowSQL(PersistentCollection $coll);
  154. /**
  155. * Gets the SQL statement used for inserting a row in the collection.
  156. *
  157. * @param PersistentCollection $coll
  158. */
  159. abstract protected function _getInsertRowSQL(PersistentCollection $coll);
  160. /**
  161. * Gets the SQL parameters for the corresponding SQL statement to insert the given
  162. * element of the given collection into the database.
  163. *
  164. * @param PersistentCollection $coll
  165. * @param mixed $element
  166. */
  167. abstract protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element);
  168. }