|
0
|
1 |
<?php |
|
|
2 |
/* |
|
|
3 |
* $Id$ |
|
|
4 |
* |
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
16 |
* |
|
|
17 |
* This software consists of voluntary contributions made by many individuals |
|
|
18 |
* and is licensed under the LGPL. For more information, see |
|
|
19 |
* <http://www.doctrine-project.org>. |
|
|
20 |
*/ |
|
|
21 |
|
|
|
22 |
namespace Doctrine\ORM\Persisters; |
|
|
23 |
|
|
|
24 |
use Doctrine\ORM\PersistentCollection, |
|
|
25 |
Doctrine\ORM\UnitOfWork; |
|
|
26 |
|
|
|
27 |
/** |
|
|
28 |
* Persister for one-to-many collections. |
|
|
29 |
* |
|
|
30 |
* IMPORTANT: |
|
|
31 |
* This persister is only used for uni-directional one-to-many mappings on a foreign key |
|
|
32 |
* (which are not yet supported). So currently this persister is not used. |
|
|
33 |
* |
|
|
34 |
* @since 2.0 |
|
|
35 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
36 |
* @todo Remove |
|
|
37 |
*/ |
|
|
38 |
class OneToManyPersister extends AbstractCollectionPersister |
|
|
39 |
{ |
|
|
40 |
/** |
|
|
41 |
* Generates the SQL UPDATE that updates a particular row's foreign |
|
|
42 |
* key to null. |
|
|
43 |
* |
|
|
44 |
* @param PersistentCollection $coll |
|
|
45 |
* @return string |
|
|
46 |
* @override |
|
|
47 |
*/ |
|
|
48 |
protected function _getDeleteRowSQL(PersistentCollection $coll) |
|
|
49 |
{ |
|
|
50 |
$mapping = $coll->getMapping(); |
|
|
51 |
$targetClass = $this->_em->getClassMetadata($mapping->getTargetEntityName()); |
|
|
52 |
$table = $targetClass->getTableName(); |
|
|
53 |
|
|
|
54 |
$ownerMapping = $targetClass->getAssociationMapping($mapping['mappedBy']); |
|
|
55 |
|
|
|
56 |
$setClause = ''; |
|
|
57 |
foreach ($ownerMapping->sourceToTargetKeyColumns as $sourceCol => $targetCol) { |
|
|
58 |
if ($setClause != '') $setClause .= ', '; |
|
|
59 |
$setClause .= "$sourceCol = NULL"; |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
$whereClause = ''; |
|
|
63 |
foreach ($targetClass->getIdentifierColumnNames() as $idColumn) { |
|
|
64 |
if ($whereClause != '') $whereClause .= ' AND '; |
|
|
65 |
$whereClause .= "$idColumn = ?"; |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
return array("UPDATE $table SET $setClause WHERE $whereClause", $this->_uow->getEntityIdentifier($element)); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
protected function _getInsertRowSQL(PersistentCollection $coll) |
|
|
72 |
{ |
|
|
73 |
return "UPDATE xxx SET foreign_key = yyy WHERE foreign_key = zzz"; |
|
|
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 |
* Gets the SQL parameters for the corresponding SQL statement to insert the given |
|
|
103 |
* element of the given collection into the database. |
|
|
104 |
* |
|
|
105 |
* @param PersistentCollection $coll |
|
|
106 |
* @param mixed $element |
|
|
107 |
*/ |
|
|
108 |
protected function _getInsertRowSQLParameters(PersistentCollection $coll, $element) |
|
|
109 |
{} |
|
|
110 |
|
|
|
111 |
/** |
|
|
112 |
* Gets the SQL parameters for the corresponding SQL statement to delete the given |
|
|
113 |
* element from the given collection. |
|
|
114 |
* |
|
|
115 |
* @param PersistentCollection $coll |
|
|
116 |
* @param mixed $element |
|
|
117 |
*/ |
|
|
118 |
protected function _getDeleteRowSQLParameters(PersistentCollection $coll, $element) |
|
|
119 |
{} |
|
|
120 |
|
|
|
121 |
/** |
|
|
122 |
* {@inheritdoc} |
|
|
123 |
*/ |
|
|
124 |
public function count(PersistentCollection $coll) |
|
|
125 |
{ |
|
|
126 |
$mapping = $coll->getMapping(); |
|
|
127 |
$targetClass = $this->_em->getClassMetadata($mapping['targetEntity']); |
|
|
128 |
$sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']); |
|
|
129 |
|
|
|
130 |
$params = array(); |
|
|
131 |
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); |
|
|
132 |
|
|
|
133 |
$where = ''; |
|
|
134 |
foreach ($targetClass->associationMappings[$mapping['mappedBy']]['joinColumns'] AS $joinColumn) { |
|
|
135 |
if ($where != '') { |
|
|
136 |
$where .= ' AND '; |
|
|
137 |
} |
|
|
138 |
$where .= $joinColumn['name'] . " = ?"; |
|
|
139 |
if ($targetClass->containsForeignIdentifier) { |
|
|
140 |
$params[] = $id[$sourceClass->getFieldForColumn($joinColumn['referencedColumnName'])]; |
|
|
141 |
} else { |
|
|
142 |
$params[] = $id[$sourceClass->fieldNames[$joinColumn['referencedColumnName']]]; |
|
|
143 |
} |
|
|
144 |
} |
|
|
145 |
|
|
|
146 |
$sql = "SELECT count(*) FROM " . $targetClass->getQuotedTableName($this->_conn->getDatabasePlatform()) . " WHERE " . $where; |
|
|
147 |
return $this->_conn->fetchColumn($sql, $params); |
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
/** |
|
|
151 |
* @param PersistentCollection $coll |
|
|
152 |
* @param int $offset |
|
|
153 |
* @param int $length |
|
|
154 |
* @return \Doctrine\Common\Collections\ArrayCollection |
|
|
155 |
*/ |
|
|
156 |
public function slice(PersistentCollection $coll, $offset, $length = null) |
|
|
157 |
{ |
|
|
158 |
$mapping = $coll->getMapping(); |
|
|
159 |
return $this->_em->getUnitOfWork() |
|
|
160 |
->getEntityPersister($mapping['targetEntity']) |
|
|
161 |
->getOneToManyCollection($mapping, $coll->getOwner(), $offset, $length); |
|
|
162 |
} |
|
|
163 |
|
|
|
164 |
/** |
|
|
165 |
* @param PersistentCollection $coll |
|
|
166 |
* @param object $element |
|
|
167 |
*/ |
|
|
168 |
public function contains(PersistentCollection $coll, $element) |
|
|
169 |
{ |
|
|
170 |
$mapping = $coll->getMapping(); |
|
|
171 |
$uow = $this->_em->getUnitOfWork(); |
|
|
172 |
|
|
|
173 |
// shortcut for new entities |
|
|
174 |
if ($uow->getEntityState($element, UnitOfWork::STATE_NEW) == UnitOfWork::STATE_NEW) { |
|
|
175 |
return false; |
|
|
176 |
} |
|
|
177 |
|
|
|
178 |
// only works with single id identifier entities. Will throw an exception in Entity Persisters |
|
|
179 |
// if that is not the case for the 'mappedBy' field. |
|
|
180 |
$id = current( $uow->getEntityIdentifier($coll->getOwner()) ); |
|
|
181 |
|
|
|
182 |
return $uow->getEntityPersister($mapping['targetEntity']) |
|
|
183 |
->exists($element, array($mapping['mappedBy'] => $id)); |
|
|
184 |
} |
|
|
185 |
} |