AbstractCollectionPersister.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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\EntityManager;
  21. use 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. * The database platform.
  44. *
  45. * @var \Doctrine\DBAL\Platforms\AbstractPlatform
  46. */
  47. protected $platform;
  48. /**
  49. * The quote strategy.
  50. *
  51. * @var \Doctrine\ORM\Mapping\QuoteStrategy
  52. */
  53. protected $quoteStrategy;
  54. /**
  55. * Initializes a new instance of a class derived from AbstractCollectionPersister.
  56. *
  57. * @param \Doctrine\ORM\EntityManager $em
  58. */
  59. public function __construct(EntityManager $em)
  60. {
  61. $this->em = $em;
  62. $this->uow = $em->getUnitOfWork();
  63. $this->conn = $em->getConnection();
  64. $this->platform = $this->conn->getDatabasePlatform();
  65. $this->quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
  66. }
  67. /**
  68. * Deletes the persistent state represented by the given collection.
  69. *
  70. * @param \Doctrine\ORM\PersistentCollection $coll
  71. *
  72. * @return void
  73. */
  74. public function delete(PersistentCollection $coll)
  75. {
  76. $mapping = $coll->getMapping();
  77. if ( ! $mapping['isOwningSide']) {
  78. return; // ignore inverse side
  79. }
  80. $sql = $this->getDeleteSQL($coll);
  81. $this->conn->executeUpdate($sql, $this->getDeleteSQLParameters($coll));
  82. }
  83. /**
  84. * Gets the SQL statement for deleting the given collection.
  85. *
  86. * @param \Doctrine\ORM\PersistentCollection $coll
  87. *
  88. * @return string
  89. */
  90. abstract protected function getDeleteSQL(PersistentCollection $coll);
  91. /**
  92. * Gets the SQL parameters for the corresponding SQL statement to delete
  93. * the given collection.
  94. *
  95. * @param \Doctrine\ORM\PersistentCollection $coll
  96. *
  97. * @return array
  98. */
  99. abstract protected function getDeleteSQLParameters(PersistentCollection $coll);
  100. /**
  101. * Updates the given collection, synchronizing its state with the database
  102. * by inserting, updating and deleting individual elements.
  103. *
  104. * @param \Doctrine\ORM\PersistentCollection $coll
  105. *
  106. * @return void
  107. */
  108. public function update(PersistentCollection $coll)
  109. {
  110. $mapping = $coll->getMapping();
  111. if ( ! $mapping['isOwningSide']) {
  112. return; // ignore inverse side
  113. }
  114. $this->deleteRows($coll);
  115. $this->insertRows($coll);
  116. }
  117. /**
  118. * Deletes rows.
  119. *
  120. * @param \Doctrine\ORM\PersistentCollection $coll
  121. *
  122. * @return void
  123. */
  124. public function deleteRows(PersistentCollection $coll)
  125. {
  126. $diff = $coll->getDeleteDiff();
  127. $sql = $this->getDeleteRowSQL($coll);
  128. foreach ($diff as $element) {
  129. $this->conn->executeUpdate($sql, $this->getDeleteRowSQLParameters($coll, $element));
  130. }
  131. }
  132. /**
  133. * Inserts rows.
  134. *
  135. * @param \Doctrine\ORM\PersistentCollection $coll
  136. *
  137. * @return void
  138. */
  139. public function insertRows(PersistentCollection $coll)
  140. {
  141. $diff = $coll->getInsertDiff();
  142. $sql = $this->getInsertRowSQL($coll);
  143. foreach ($diff as $element) {
  144. $this->conn->executeUpdate($sql, $this->getInsertRowSQLParameters($coll, $element));
  145. }
  146. }
  147. /**
  148. * Counts the size of this persistent collection.
  149. *
  150. * @param \Doctrine\ORM\PersistentCollection $coll
  151. *
  152. * @return integer
  153. *
  154. * @throws \BadMethodCallException
  155. */
  156. public function count(PersistentCollection $coll)
  157. {
  158. throw new \BadMethodCallException("Counting the size of this persistent collection is not supported by this CollectionPersister.");
  159. }
  160. /**
  161. * Slices elements.
  162. *
  163. * @param \Doctrine\ORM\PersistentCollection $coll
  164. * @param integer $offset
  165. * @param integer $length
  166. *
  167. * @return array
  168. *
  169. * @throws \BadMethodCallException
  170. */
  171. public function slice(PersistentCollection $coll, $offset, $length = null)
  172. {
  173. throw new \BadMethodCallException("Slicing elements is not supported by this CollectionPersister.");
  174. }
  175. /**
  176. * Checks for existence of an element.
  177. *
  178. * @param \Doctrine\ORM\PersistentCollection $coll
  179. * @param object $element
  180. *
  181. * @return boolean
  182. *
  183. * @throws \BadMethodCallException
  184. */
  185. public function contains(PersistentCollection $coll, $element)
  186. {
  187. throw new \BadMethodCallException("Checking for existence of an element is not supported by this CollectionPersister.");
  188. }
  189. /**
  190. * Checks for existence of a key.
  191. *
  192. * @param \Doctrine\ORM\PersistentCollection $coll
  193. * @param mixed $key
  194. *
  195. * @return boolean
  196. *
  197. * @throws \BadMethodCallException
  198. */
  199. public function containsKey(PersistentCollection $coll, $key)
  200. {
  201. throw new \BadMethodCallException("Checking for existence of a key is not supported by this CollectionPersister.");
  202. }
  203. /**
  204. * Removes an element.
  205. *
  206. * @param \Doctrine\ORM\PersistentCollection $coll
  207. * @param object $element
  208. *
  209. * @return mixed
  210. *
  211. * @throws \BadMethodCallException
  212. */
  213. public function removeElement(PersistentCollection $coll, $element)
  214. {
  215. throw new \BadMethodCallException("Removing an element is not supported by this CollectionPersister.");
  216. }
  217. /**
  218. * Removes an element by key.
  219. *
  220. * @param \Doctrine\ORM\PersistentCollection $coll
  221. * @param mixed $key
  222. *
  223. * @return void
  224. *
  225. * @throws \BadMethodCallException
  226. */
  227. public function removeKey(PersistentCollection $coll, $key)
  228. {
  229. throw new \BadMethodCallException("Removing a key is not supported by this CollectionPersister.");
  230. }
  231. /**
  232. * Gets an element by key.
  233. *
  234. * @param \Doctrine\ORM\PersistentCollection $coll
  235. * @param mixed $index
  236. *
  237. * @return mixed
  238. *
  239. * @throws \BadMethodCallException
  240. */
  241. public function get(PersistentCollection $coll, $index)
  242. {
  243. throw new \BadMethodCallException("Selecting a collection by index is not supported by this CollectionPersister.");
  244. }
  245. /**
  246. * Gets the SQL statement used for deleting a row from the collection.
  247. *
  248. * @param \Doctrine\ORM\PersistentCollection $coll
  249. *
  250. * @return string
  251. */
  252. abstract protected function getDeleteRowSQL(PersistentCollection $coll);
  253. /**
  254. * Gets the SQL parameters for the corresponding SQL statement to delete the given
  255. * element from the given collection.
  256. *
  257. * @param \Doctrine\ORM\PersistentCollection $coll
  258. * @param mixed $element
  259. *
  260. * @return array
  261. */
  262. abstract protected function getDeleteRowSQLParameters(PersistentCollection $coll, $element);
  263. /**
  264. * Gets the SQL statement used for updating a row in the collection.
  265. *
  266. * @param \Doctrine\ORM\PersistentCollection $coll
  267. *
  268. * @return string
  269. */
  270. abstract protected function getUpdateRowSQL(PersistentCollection $coll);
  271. /**
  272. * Gets the SQL statement used for inserting a row in the collection.
  273. *
  274. * @param \Doctrine\ORM\PersistentCollection $coll
  275. *
  276. * @return string
  277. */
  278. abstract protected function getInsertRowSQL(PersistentCollection $coll);
  279. /**
  280. * Gets the SQL parameters for the corresponding SQL statement to insert the given
  281. * element of the given collection into the database.
  282. *
  283. * @param \Doctrine\ORM\PersistentCollection $coll
  284. * @param mixed $element
  285. *
  286. * @return array
  287. */
  288. abstract protected function getInsertRowSQLParameters(PersistentCollection $coll, $element);
  289. }