|
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 |
|
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 * Initializes a new instance of a class derived from AbstractCollectionPersister. |
|
50 * |
|
51 * @param Doctrine\ORM\EntityManager $em |
|
52 */ |
|
53 public function __construct(EntityManager $em) |
|
54 { |
|
55 $this->_em = $em; |
|
56 $this->_uow = $em->getUnitOfWork(); |
|
57 $this->_conn = $em->getConnection(); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Deletes the persistent state represented by the given collection. |
|
62 * |
|
63 * @param PersistentCollection $coll |
|
64 */ |
|
65 public function delete(PersistentCollection $coll) |
|
66 { |
|
67 $mapping = $coll->getMapping(); |
|
68 if ( ! $mapping['isOwningSide']) { |
|
69 return; // ignore inverse side |
|
70 } |
|
71 $sql = $this->_getDeleteSQL($coll); |
|
72 $this->_conn->executeUpdate($sql, $this->_getDeleteSQLParameters($coll)); |
|
73 } |
|
74 |
|
75 /** |
|
76 * Gets the SQL statement for deleting the given collection. |
|
77 * |
|
78 * @param PersistentCollection $coll |
|
79 */ |
|
80 abstract protected function _getDeleteSQL(PersistentCollection $coll); |
|
81 |
|
82 /** |
|
83 * Gets the SQL parameters for the corresponding SQL statement to delete |
|
84 * the given collection. |
|
85 * |
|
86 * @param PersistentCollection $coll |
|
87 */ |
|
88 abstract protected function _getDeleteSQLParameters(PersistentCollection $coll); |
|
89 |
|
90 /** |
|
91 * Updates the given collection, synchronizing it's state with the database |
|
92 * by inserting, updating and deleting individual elements. |
|
93 * |
|
94 * @param PersistentCollection $coll |
|
95 */ |
|
96 public function update(PersistentCollection $coll) |
|
97 { |
|
98 $mapping = $coll->getMapping(); |
|
99 if ( ! $mapping['isOwningSide']) { |
|
100 return; // ignore inverse side |
|
101 } |
|
102 $this->deleteRows($coll); |
|
103 //$this->updateRows($coll); |
|
104 $this->insertRows($coll); |
|
105 } |
|
106 |
|
107 public function deleteRows(PersistentCollection $coll) |
|
108 { |
|
109 $deleteDiff = $coll->getDeleteDiff(); |
|
110 $sql = $this->_getDeleteRowSQL($coll); |
|
111 foreach ($deleteDiff as $element) { |
|
112 $this->_conn->executeUpdate($sql, $this->_getDeleteRowSQLParameters($coll, $element)); |
|
113 } |
|
114 } |
|
115 |
|
116 //public function updateRows(PersistentCollection $coll) |
|
117 //{} |
|
118 |
|
119 public function insertRows(PersistentCollection $coll) |
|
120 { |
|
121 $insertDiff = $coll->getInsertDiff(); |
|
122 $sql = $this->_getInsertRowSQL($coll); |
|
123 foreach ($insertDiff as $element) { |
|
124 $this->_conn->executeUpdate($sql, $this->_getInsertRowSQLParameters($coll, $element)); |
|
125 } |
|
126 } |
|
127 |
|
128 public function count(PersistentCollection $coll) |
|
129 { |
|
130 throw new \BadMethodCallException("Counting the size of this persistent collection is not supported by this CollectionPersister."); |
|
131 } |
|
132 |
|
133 public function slice(PersistentCollection $coll, $offset, $length = null) |
|
134 { |
|
135 throw new \BadMethodCallException("Slicing elements is not supported by this CollectionPersister."); |
|
136 } |
|
137 |
|
138 public function contains(PersistentCollection $coll, $element) |
|
139 { |
|
140 throw new \BadMethodCallException("Checking for existance of an element is not supported by this CollectionPersister."); |
|
141 } |
|
142 |
|
143 public function containsKey(PersistentCollection $coll, $key) |
|
144 { |
|
145 throw new \BadMethodCallException("Checking for existance of a key is not supported by this CollectionPersister."); |
|
146 } |
|
147 |
|
148 public function get(PersistentCollection $coll, $index) |
|
149 { |
|
150 throw new \BadMethodCallException("Selecting a collection by index is not supported by this CollectionPersister."); |
|
151 } |
|
152 |
|
153 /** |
|
154 * Gets the SQL statement used for deleting a row from the collection. |
|
155 * |
|
156 * @param PersistentCollection $coll |
|
157 */ |
|
158 abstract protected function _getDeleteRowSQL(PersistentCollection $coll); |
|
159 |
|
160 /** |
|
161 * Gets the SQL parameters for the corresponding SQL statement to delete the given |
|
162 * element from the given collection. |
|
163 * |
|
164 * @param PersistentCollection $coll |
|
165 * @param mixed $element |
|
166 */ |
|
167 abstract protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element); |
|
168 |
|
169 /** |
|
170 * Gets the SQL statement used for updating a row in the collection. |
|
171 * |
|
172 * @param PersistentCollection $coll |
|
173 */ |
|
174 abstract protected function _getUpdateRowSQL(PersistentCollection $coll); |
|
175 |
|
176 /** |
|
177 * Gets the SQL statement used for inserting a row in the collection. |
|
178 * |
|
179 * @param PersistentCollection $coll |
|
180 */ |
|
181 abstract protected function _getInsertRowSQL(PersistentCollection $coll); |
|
182 |
|
183 /** |
|
184 * Gets the SQL parameters for the corresponding SQL statement to insert the given |
|
185 * element of the given collection into the database. |
|
186 * |
|
187 * @param PersistentCollection $coll |
|
188 * @param mixed $element |
|
189 */ |
|
190 abstract protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element); |
|
191 } |