AbstractCollectionPersister.php 5.14 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;
use 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
    protected $_em;
    protected $_conn;
37
    protected $_uow;
38

39 40 41 42 43
    /**
     * Initializes a new instance of a class derived from {@link AbstractCollectionPersister}.
     *
     * @param Doctrine\ORM\EntityManager $em
     */
44 45 46
    public function __construct(EntityManager $em)
    {
        $this->_em = $em;
47
        $this->_uow = $em->getUnitOfWork();
48 49 50
        $this->_conn = $em->getConnection();
    }

51
    /*public function recreate(PersistentCollection $coll)
romanb's avatar
romanb committed
52 53 54 55 56
    {
        if ($coll->getRelation()->isInverseSide()) {
            return;
        }
        //...
57
    }*/
58 59 60 61 62 63

    /**
     * Deletes the persistent state represented by the given collection.
     *
     * @param PersistentCollection $coll
     */
64
    public function delete(PersistentCollection $coll)
romanb's avatar
romanb committed
65
    {
66 67
        if ($coll->getMapping()->isInverseSide()) {
            return; // ignore inverse side
romanb's avatar
romanb committed
68
        }
69
        $sql = $this->_getDeleteSql($coll);
70
        $this->_conn->executeUpdate($sql, $this->_getDeleteSqlParameters($coll));
romanb's avatar
romanb committed
71
    }
72

73 74 75 76 77
    /**
     * Gets the SQL statement for deleting the given collection.
     *
     * @param PersistentCollection $coll
     */
78
    abstract protected function _getDeleteSql(PersistentCollection $coll);
79 80 81 82 83 84 85

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

88 89 90 91 92 93
    /**
     * Updates the given collection, synchronizing it's state with the database
     * by inserting, updating and deleting individual elements.
     *
     * @param PersistentCollection $coll
     */
94 95
    public function update(PersistentCollection $coll)
    {
96 97 98
        if ($coll->getMapping()->isInverseSide()) {
            return; // ignore inverse side
        }
99
        $this->deleteRows($coll);
100
        //$this->updateRows($coll);
101 102
        $this->insertRows($coll);
    }
romanb's avatar
romanb committed
103
    
104
    public function deleteRows(PersistentCollection $coll)
105
    {        
106 107 108
        $deleteDiff = $coll->getDeleteDiff();
        $sql = $this->_getDeleteRowSql($coll);
        foreach ($deleteDiff as $element) {
109
            $this->_conn->executeUpdate($sql, $this->_getDeleteRowSqlParameters($coll, $element));
110
        }
romanb's avatar
romanb committed
111 112
    }
    
113 114
    //public function updateRows(PersistentCollection $coll)
    //{}
romanb's avatar
romanb committed
115
    
116
    public function insertRows(PersistentCollection $coll)
117
    {
118 119 120
        $insertDiff = $coll->getInsertDiff();
        $sql = $this->_getInsertRowSql($coll);
        foreach ($insertDiff as $element) {
121
            $this->_conn->executeUpdate($sql, $this->_getInsertRowSqlParameters($coll, $element));
122
        }
123 124
    }

125 126 127 128 129 130
    /**
     * Gets the SQL statement used for deleting a row from the collection.
     * 
     * @param PersistentCollection $coll
     */
    abstract protected function _getDeleteRowSql(PersistentCollection $coll);
131

132 133 134 135 136 137 138
    /**
     * Gets the SQL parameters for the corresponding SQL statement to delete the given
     * element from the given collection.
     *
     * @param PersistentCollection $coll
     * @param mixed $element
     */
139 140
    abstract protected function _getDeleteRowSqlParameters(PersistentCollection $coll, $element);

141 142 143 144 145
    /**
     * Gets the SQL statement used for updating a row in the collection.
     *
     * @param PersistentCollection $coll
     */
146
    abstract protected function _getUpdateRowSql(PersistentCollection $coll);
147 148

    /**
149
     * Gets the SQL statement used for inserting a row in the collection.
150 151 152
     *
     * @param PersistentCollection $coll
     */
153
    abstract protected function _getInsertRowSql(PersistentCollection $coll);
154 155 156 157 158 159 160 161

    /**
     * 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
     */
162
    abstract protected function _getInsertRowSqlParameters(PersistentCollection $coll, $element);
163
}