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.
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>.
20 namespace Doctrine\ORM\Persisters;
22 use Doctrine\ORM\EntityManager,
23 Doctrine\ORM\PersistentCollection;
26 * Base class for all collection persisters.
29 * @author Roman Borschel <roman@code-factory.org>
31 abstract class AbstractCollectionPersister
39 * @var \Doctrine\DBAL\Connection
44 * @var \Doctrine\ORM\UnitOfWork
49 * The database platform.
51 * @var \Doctrine\DBAL\Platforms\AbstractPlatform
58 * @var \Doctrine\ORM\Mapping\QuoteStrategy
60 protected $quoteStrategy;
63 * Initializes a new instance of a class derived from AbstractCollectionPersister.
65 * @param \Doctrine\ORM\EntityManager $em
67 public function __construct(EntityManager $em)
70 $this->_uow = $em->getUnitOfWork();
71 $this->_conn = $em->getConnection();
72 $this->platform = $this->_conn->getDatabasePlatform();
73 $this->quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
77 * Deletes the persistent state represented by the given collection.
79 * @param PersistentCollection $coll
81 public function delete(PersistentCollection $coll)
83 $mapping = $coll->getMapping();
85 if ( ! $mapping['isOwningSide']) {
86 return; // ignore inverse side
89 $sql = $this->_getDeleteSQL($coll);
90 $this->_conn->executeUpdate($sql, $this->_getDeleteSQLParameters($coll));
94 * Gets the SQL statement for deleting the given collection.
96 * @param PersistentCollection $coll
98 abstract protected function _getDeleteSQL(PersistentCollection $coll);
101 * Gets the SQL parameters for the corresponding SQL statement to delete
102 * the given collection.
104 * @param PersistentCollection $coll
106 abstract protected function _getDeleteSQLParameters(PersistentCollection $coll);
109 * Updates the given collection, synchronizing it's state with the database
110 * by inserting, updating and deleting individual elements.
112 * @param PersistentCollection $coll
114 public function update(PersistentCollection $coll)
116 $mapping = $coll->getMapping();
118 if ( ! $mapping['isOwningSide']) {
119 return; // ignore inverse side
122 $this->deleteRows($coll);
123 //$this->updateRows($coll);
124 $this->insertRows($coll);
127 public function deleteRows(PersistentCollection $coll)
129 $deleteDiff = $coll->getDeleteDiff();
130 $sql = $this->_getDeleteRowSQL($coll);
132 foreach ($deleteDiff as $element) {
133 $this->_conn->executeUpdate($sql, $this->_getDeleteRowSQLParameters($coll, $element));
137 //public function updateRows(PersistentCollection $coll)
140 public function insertRows(PersistentCollection $coll)
142 $insertDiff = $coll->getInsertDiff();
143 $sql = $this->_getInsertRowSQL($coll);
145 foreach ($insertDiff as $element) {
146 $this->_conn->executeUpdate($sql, $this->_getInsertRowSQLParameters($coll, $element));
150 public function count(PersistentCollection $coll)
152 throw new \BadMethodCallException("Counting the size of this persistent collection is not supported by this CollectionPersister.");
155 public function slice(PersistentCollection $coll, $offset, $length = null)
157 throw new \BadMethodCallException("Slicing elements is not supported by this CollectionPersister.");
160 public function contains(PersistentCollection $coll, $element)
162 throw new \BadMethodCallException("Checking for existance of an element is not supported by this CollectionPersister.");
165 public function containsKey(PersistentCollection $coll, $key)
167 throw new \BadMethodCallException("Checking for existance of a key is not supported by this CollectionPersister.");
170 public function removeElement(PersistentCollection $coll, $element)
172 throw new \BadMethodCallException("Removing an element is not supported by this CollectionPersister.");
175 public function removeKey(PersistentCollection $coll, $key)
177 throw new \BadMethodCallException("Removing a key is not supported by this CollectionPersister.");
180 public function get(PersistentCollection $coll, $index)
182 throw new \BadMethodCallException("Selecting a collection by index is not supported by this CollectionPersister.");
186 * Gets the SQL statement used for deleting a row from the collection.
188 * @param PersistentCollection $coll
190 abstract protected function _getDeleteRowSQL(PersistentCollection $coll);
193 * Gets the SQL parameters for the corresponding SQL statement to delete the given
194 * element from the given collection.
196 * @param PersistentCollection $coll
197 * @param mixed $element
199 abstract protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element);
202 * Gets the SQL statement used for updating a row in the collection.
204 * @param PersistentCollection $coll
206 abstract protected function _getUpdateRowSQL(PersistentCollection $coll);
209 * Gets the SQL statement used for inserting a row in the collection.
211 * @param PersistentCollection $coll
213 abstract protected function _getInsertRowSQL(PersistentCollection $coll);
216 * Gets the SQL parameters for the corresponding SQL statement to insert the given
217 * element of the given collection into the database.
219 * @param PersistentCollection $coll
220 * @param mixed $element
222 abstract protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element);