AbstractCollectionPersister.php 5.3 KB
Newer Older
romanb's avatar
romanb committed
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */
romanb's avatar
romanb committed
21

22 23
namespace Doctrine\ORM\Persisters;

24 25
use Doctrine\ORM\EntityManager,
    Doctrine\ORM\PersistentCollection;
26

27 28 29 30 31 32
/**
 * Base class for all collection persisters.
 *
 * @since 2.0
 * @author Roman Borschel <roman@code-factory.org>
 */
33
abstract class AbstractCollectionPersister
romanb's avatar
romanb committed
34
{
35 36 37 38
    /**
     *
     * @var EntityManager
     */
39
    protected $_em;
40 41 42 43

    /**
     * @var \Doctrine\DBAL\Connection
     */
44
    protected $_conn;
45 46 47 48 49

    /**
     *
     * @var \Doctrine\ORM\UnitOfWork
     */
50
    protected $_uow;
51

52 53 54 55 56
    /**
     * Initializes a new instance of a class derived from {@link AbstractCollectionPersister}.
     *
     * @param Doctrine\ORM\EntityManager $em
     */
57 58 59
    public function __construct(EntityManager $em)
    {
        $this->_em = $em;
60
        $this->_uow = $em->getUnitOfWork();
61 62 63
        $this->_conn = $em->getConnection();
    }

64
    /*public function recreate(PersistentCollection $coll)
romanb's avatar
romanb committed
65 66 67 68 69
    {
        if ($coll->getRelation()->isInverseSide()) {
            return;
        }
        //...
70
    }*/
71 72 73 74 75 76

    /**
     * Deletes the persistent state represented by the given collection.
     *
     * @param PersistentCollection $coll
     */
77
    public function delete(PersistentCollection $coll)
romanb's avatar
romanb committed
78
    {
79 80
        if ($coll->getMapping()->isInverseSide()) {
            return; // ignore inverse side
romanb's avatar
romanb committed
81
        }
82
        $sql = $this->_getDeleteSql($coll);
83
        $this->_conn->executeUpdate($sql, $this->_getDeleteSqlParameters($coll));
romanb's avatar
romanb committed
84
    }
85

86 87 88 89 90
    /**
     * Gets the SQL statement for deleting the given collection.
     *
     * @param PersistentCollection $coll
     */
91
    abstract protected function _getDeleteSql(PersistentCollection $coll);
92 93 94 95 96 97 98

    /**
     * Gets the SQL parameters for the corresponding SQL statement to delete
     * the given collection.
     *
     * @param PersistentCollection $coll
     */
99 100
    abstract protected function _getDeleteSqlParameters(PersistentCollection $coll);

101 102 103 104 105 106
    /**
     * Updates the given collection, synchronizing it's state with the database
     * by inserting, updating and deleting individual elements.
     *
     * @param PersistentCollection $coll
     */
107 108
    public function update(PersistentCollection $coll)
    {
109 110 111
        if ($coll->getMapping()->isInverseSide()) {
            return; // ignore inverse side
        }
112
        $this->deleteRows($coll);
113
        //$this->updateRows($coll);
114 115
        $this->insertRows($coll);
    }
romanb's avatar
romanb committed
116
    
117
    public function deleteRows(PersistentCollection $coll)
118
    {        
119 120 121
        $deleteDiff = $coll->getDeleteDiff();
        $sql = $this->_getDeleteRowSql($coll);
        foreach ($deleteDiff as $element) {
122
            $this->_conn->executeUpdate($sql, $this->_getDeleteRowSqlParameters($coll, $element));
123
        }
romanb's avatar
romanb committed
124 125
    }
    
126 127
    //public function updateRows(PersistentCollection $coll)
    //{}
romanb's avatar
romanb committed
128
    
129
    public function insertRows(PersistentCollection $coll)
130
    {
131 132 133
        $insertDiff = $coll->getInsertDiff();
        $sql = $this->_getInsertRowSql($coll);
        foreach ($insertDiff as $element) {
134
            $this->_conn->executeUpdate($sql, $this->_getInsertRowSqlParameters($coll, $element));
135
        }
136 137
    }

138 139 140 141 142 143
    /**
     * Gets the SQL statement used for deleting a row from the collection.
     * 
     * @param PersistentCollection $coll
     */
    abstract protected function _getDeleteRowSql(PersistentCollection $coll);
144

145 146 147 148 149 150 151
    /**
     * Gets the SQL parameters for the corresponding SQL statement to delete the given
     * element from the given collection.
     *
     * @param PersistentCollection $coll
     * @param mixed $element
     */
152 153
    abstract protected function _getDeleteRowSqlParameters(PersistentCollection $coll, $element);

154 155 156 157 158
    /**
     * Gets the SQL statement used for updating a row in the collection.
     *
     * @param PersistentCollection $coll
     */
159
    abstract protected function _getUpdateRowSql(PersistentCollection $coll);
160 161

    /**
162
     * Gets the SQL statement used for inserting a row in the collection.
163 164 165
     *
     * @param PersistentCollection $coll
     */
166
    abstract protected function _getInsertRowSql(PersistentCollection $coll);
167 168 169 170 171 172 173 174

    /**
     * Gets the SQL parameters for the corresponding SQL statement to insert the given
     * element of the given collection into the database.
     *
     * @param PersistentCollection $coll
     * @param mixed $element
     */
175
    abstract protected function _getInsertRowSqlParameters(PersistentCollection $coll, $element);
176
}