Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Persisters / AbstractCollectionPersister.php
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
20 namespace Doctrine\ORM\Persisters;
21
22 use Doctrine\ORM\EntityManager,
23     Doctrine\ORM\PersistentCollection;
24
25 /**
26  * Base class for all collection persisters.
27  *
28  * @since 2.0
29  * @author Roman Borschel <roman@code-factory.org>
30  */
31 abstract class AbstractCollectionPersister
32 {
33     /**
34      * @var EntityManager
35      */
36     protected $_em;
37
38     /**
39      * @var \Doctrine\DBAL\Connection
40      */
41     protected $_conn;
42
43     /**
44      * @var \Doctrine\ORM\UnitOfWork
45      */
46     protected $_uow;
47
48     /**
49      * The database platform.
50      *
51      * @var \Doctrine\DBAL\Platforms\AbstractPlatform
52      */
53     protected $platform;
54     
55     /**
56      * The quote strategy.
57      *
58      * @var \Doctrine\ORM\Mapping\QuoteStrategy
59      */
60     protected $quoteStrategy;
61
62     /**
63      * Initializes a new instance of a class derived from AbstractCollectionPersister.
64      *
65      * @param \Doctrine\ORM\EntityManager $em
66      */
67     public function __construct(EntityManager $em)
68     {
69         $this->_em              = $em;
70         $this->_uow             = $em->getUnitOfWork();
71         $this->_conn            = $em->getConnection();
72         $this->platform         = $this->_conn->getDatabasePlatform();
73         $this->quoteStrategy    = $em->getConfiguration()->getQuoteStrategy();
74     }
75
76     /**
77      * Deletes the persistent state represented by the given collection.
78      *
79      * @param PersistentCollection $coll
80      */
81     public function delete(PersistentCollection $coll)
82     {
83         $mapping = $coll->getMapping();
84
85         if ( ! $mapping['isOwningSide']) {
86             return; // ignore inverse side
87         }
88
89         $sql = $this->_getDeleteSQL($coll);
90         $this->_conn->executeUpdate($sql, $this->_getDeleteSQLParameters($coll));
91     }
92
93     /**
94      * Gets the SQL statement for deleting the given collection.
95      *
96      * @param PersistentCollection $coll
97      */
98     abstract protected function _getDeleteSQL(PersistentCollection $coll);
99
100     /**
101      * Gets the SQL parameters for the corresponding SQL statement to delete
102      * the given collection.
103      *
104      * @param PersistentCollection $coll
105      */
106     abstract protected function _getDeleteSQLParameters(PersistentCollection $coll);
107
108     /**
109      * Updates the given collection, synchronizing it's state with the database
110      * by inserting, updating and deleting individual elements.
111      *
112      * @param PersistentCollection $coll
113      */
114     public function update(PersistentCollection $coll)
115     {
116         $mapping = $coll->getMapping();
117
118         if ( ! $mapping['isOwningSide']) {
119             return; // ignore inverse side
120         }
121
122         $this->deleteRows($coll);
123         //$this->updateRows($coll);
124         $this->insertRows($coll);
125     }
126
127     public function deleteRows(PersistentCollection $coll)
128     {
129         $deleteDiff = $coll->getDeleteDiff();
130         $sql = $this->_getDeleteRowSQL($coll);
131
132         foreach ($deleteDiff as $element) {
133             $this->_conn->executeUpdate($sql, $this->_getDeleteRowSQLParameters($coll, $element));
134         }
135     }
136
137     //public function updateRows(PersistentCollection $coll)
138     //{}
139
140     public function insertRows(PersistentCollection $coll)
141     {
142         $insertDiff = $coll->getInsertDiff();
143         $sql = $this->_getInsertRowSQL($coll);
144
145         foreach ($insertDiff as $element) {
146             $this->_conn->executeUpdate($sql, $this->_getInsertRowSQLParameters($coll, $element));
147         }
148     }
149
150     public function count(PersistentCollection $coll)
151     {
152         throw new \BadMethodCallException("Counting the size of this persistent collection is not supported by this CollectionPersister.");
153     }
154
155     public function slice(PersistentCollection $coll, $offset, $length = null)
156     {
157         throw new \BadMethodCallException("Slicing elements is not supported by this CollectionPersister.");
158     }
159
160     public function contains(PersistentCollection $coll, $element)
161     {
162         throw new \BadMethodCallException("Checking for existance of an element is not supported by this CollectionPersister.");
163     }
164
165     public function containsKey(PersistentCollection $coll, $key)
166     {
167         throw new \BadMethodCallException("Checking for existance of a key is not supported by this CollectionPersister.");
168     }
169
170     public function removeElement(PersistentCollection $coll, $element)
171     {
172         throw new \BadMethodCallException("Removing an element is not supported by this CollectionPersister.");
173     }
174
175     public function removeKey(PersistentCollection $coll, $key)
176     {
177         throw new \BadMethodCallException("Removing a key is not supported by this CollectionPersister.");
178     }
179
180     public function get(PersistentCollection $coll, $index)
181     {
182         throw new \BadMethodCallException("Selecting a collection by index is not supported by this CollectionPersister.");
183     }
184
185     /**
186      * Gets the SQL statement used for deleting a row from the collection.
187      *
188      * @param PersistentCollection $coll
189      */
190     abstract protected function _getDeleteRowSQL(PersistentCollection $coll);
191
192     /**
193      * Gets the SQL parameters for the corresponding SQL statement to delete the given
194      * element from the given collection.
195      *
196      * @param PersistentCollection $coll
197      * @param mixed $element
198      */
199     abstract protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element);
200
201     /**
202      * Gets the SQL statement used for updating a row in the collection.
203      *
204      * @param PersistentCollection $coll
205      */
206     abstract protected function _getUpdateRowSQL(PersistentCollection $coll);
207
208     /**
209      * Gets the SQL statement used for inserting a row in the collection.
210      *
211      * @param PersistentCollection $coll
212      */
213     abstract protected function _getInsertRowSQL(PersistentCollection $coll);
214
215     /**
216      * Gets the SQL parameters for the corresponding SQL statement to insert the given
217      * element of the given collection into the database.
218      *
219      * @param PersistentCollection $coll
220      * @param mixed $element
221      */
222     abstract protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element);
223 }