source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/ORM/Persisters/OneToManyPersister.php @ 348

Last change on this file since 348 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 7.3 KB
Line 
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
20namespace Doctrine\ORM\Persisters;
21
22use Doctrine\ORM\PersistentCollection,
23    Doctrine\ORM\UnitOfWork;
24
25/**
26 * Persister for one-to-many collections.
27 *
28 * @author  Roman Borschel <roman@code-factory.org>
29 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
30 * @author  Alexander <iam.asm89@gmail.com>
31 * @since   2.0
32 */
33class OneToManyPersister extends AbstractCollectionPersister
34{
35    /**
36     * Generates the SQL UPDATE that updates a particular row's foreign
37     * key to null.
38     *
39     * @param PersistentCollection $coll
40     * @return string
41     * @override
42     */
43    protected function _getDeleteRowSQL(PersistentCollection $coll)
44    {
45        $mapping = $coll->getMapping();
46        $class   = $this->_em->getClassMetadata($mapping['targetEntity']);
47
48        return 'DELETE FROM ' . $class->getQuotedTableName($this->_conn->getDatabasePlatform())
49             . ' WHERE ' . implode('= ? AND ', $class->getIdentifierColumnNames()) . ' = ?';
50    }
51
52    /**
53     * {@inheritdoc}
54     *
55     */
56    protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element)
57    {
58        return array_values($this->_uow->getEntityIdentifier($element));
59    }
60
61    protected function _getInsertRowSQL(PersistentCollection $coll)
62    {
63        return "UPDATE xxx SET foreign_key = yyy WHERE foreign_key = zzz";
64    }
65
66    /**
67     * Gets the SQL parameters for the corresponding SQL statement to insert the given
68     * element of the given collection into the database.
69     *
70     * @param PersistentCollection $coll
71     * @param mixed $element
72     */
73    protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element)
74    {}
75
76    /* Not used for OneToManyPersister */
77    protected function _getUpdateRowSQL(PersistentCollection $coll)
78    {
79        return;
80    }
81
82    /**
83     * Generates the SQL UPDATE that updates all the foreign keys to null.
84     *
85     * @param PersistentCollection $coll
86     */
87    protected function _getDeleteSQL(PersistentCollection $coll)
88    {
89
90    }
91
92    /**
93     * Gets the SQL parameters for the corresponding SQL statement to delete
94     * the given collection.
95     *
96     * @param PersistentCollection $coll
97     */
98    protected function _getDeleteSQLParameters(PersistentCollection $coll)
99    {}
100
101    /**
102     * {@inheritdoc}
103     */
104    public function count(PersistentCollection $coll)
105    {
106        $mapping     = $coll->getMapping();
107        $targetClass = $this->_em->getClassMetadata($mapping['targetEntity']);
108        $sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']);
109        $id          = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
110
111        $whereClauses = array();
112        $params       = array();
113
114        foreach ($targetClass->associationMappings[$mapping['mappedBy']]['joinColumns'] AS $joinColumn) {
115            $whereClauses[] = $joinColumn['name'] . ' = ?';
116
117            $params[] = ($targetClass->containsForeignIdentifier)
118                ? $id[$sourceClass->getFieldForColumn($joinColumn['referencedColumnName'])]
119                : $id[$sourceClass->fieldNames[$joinColumn['referencedColumnName']]];
120        }
121
122        $filterTargetClass = $this->_em->getClassMetadata($targetClass->rootEntityName);
123        foreach ($this->_em->getFilters()->getEnabledFilters() as $filter) {
124            if ($filterExpr = $filter->addFilterConstraint($filterTargetClass, 't')) {
125                $whereClauses[] = '(' . $filterExpr . ')';
126            }
127        }
128
129        $sql = 'SELECT count(*)'
130             . ' FROM ' . $targetClass->getQuotedTableName($this->_conn->getDatabasePlatform()) . ' t'
131             . ' WHERE ' . implode(' AND ', $whereClauses);
132
133        return $this->_conn->fetchColumn($sql, $params);
134    }
135
136    /**
137     * @param PersistentCollection $coll
138     * @param int $offset
139     * @param int $length
140     * @return \Doctrine\Common\Collections\ArrayCollection
141     */
142    public function slice(PersistentCollection $coll, $offset, $length = null)
143    {
144        $mapping   = $coll->getMapping();
145        $uow       = $this->_em->getUnitOfWork();
146        $persister = $uow->getEntityPersister($mapping['targetEntity']);
147
148        return $persister->getOneToManyCollection($mapping, $coll->getOwner(), $offset, $length);
149    }
150
151    /**
152     * @param PersistentCollection $coll
153     * @param object $element
154     * @return boolean
155     */
156    public function contains(PersistentCollection $coll, $element)
157    {
158        $mapping = $coll->getMapping();
159        $uow     = $this->_em->getUnitOfWork();
160
161        // shortcut for new entities
162        $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
163
164        if ($entityState === UnitOfWork::STATE_NEW) {
165            return false;
166        }
167
168        // Entity is scheduled for inclusion
169        if ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element)) {
170            return false;
171        }
172
173        $persister = $uow->getEntityPersister($mapping['targetEntity']);
174
175        // only works with single id identifier entities. Will throw an
176        // exception in Entity Persisters if that is not the case for the
177        // 'mappedBy' field.
178        $id = current( $uow->getEntityIdentifier($coll->getOwner()));
179
180        return $persister->exists($element, array($mapping['mappedBy'] => $id));
181    }
182
183    /**
184     * @param PersistentCollection $coll
185     * @param object $element
186     * @return boolean
187     */
188    public function removeElement(PersistentCollection $coll, $element)
189    {
190        $uow = $this->_em->getUnitOfWork();
191
192        // shortcut for new entities
193        $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
194
195        if ($entityState === UnitOfWork::STATE_NEW) {
196            return false;
197        }
198
199        // If Entity is scheduled for inclusion, it is not in this collection.
200        // We can assure that because it would have return true before on array check
201        if ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element)) {
202            return false;
203        }
204
205        $mapping = $coll->getMapping();
206        $class   = $this->_em->getClassMetadata($mapping['targetEntity']);
207        $sql     = 'DELETE FROM ' . $class->getQuotedTableName($this->_conn->getDatabasePlatform())
208                 . ' WHERE ' . implode('= ? AND ', $class->getIdentifierColumnNames()) . ' = ?';
209
210        return (bool) $this->_conn->executeUpdate($sql, $this->_getDeleteRowSQLParameters($coll, $element));
211    }
212}
Note: See TracBrowser for help on using the repository browser.