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