-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AbstractEntityPersister.php
240 lines (219 loc) · 7.78 KB
/
AbstractEntityPersister.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/*
* $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>.
*/
namespace Doctrine\ORM\Persisters;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadata;
/**
* Base class for all EntityPersisters.
*
* @author Roman Borschel <roman@code-factory.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision: 3406 $
* @link www.doctrine-project.org
* @since 2.0
*/
abstract class AbstractEntityPersister
{
/**
* Metadata object that describes the mapping of the mapped entity class.
*
* @var Doctrine\ORM\Mapping\ClassMetadata
*/
protected $_classMetadata;
/**
* The name of the Entity the persister is used for.
*
* @var string
*/
protected $_entityName;
/**
* The Connection instance.
*
* @var Doctrine\DBAL\Connection $conn
*/
protected $_conn;
/**
* The EntityManager instance.
*
* @var Doctrine\ORM\EntityManager
*/
protected $_em;
protected $_queuedInserts = array();
/**
* Initializes a new instance of a class derived from AbstractEntityPersister
* that uses the given EntityManager and persists instances of the class described
* by the given class metadata descriptor.
*/
public function __construct(EntityManager $em, ClassMetadata $classMetadata)
{
$this->_em = $em;
$this->_entityName = $classMetadata->getClassName();
$this->_conn = $em->getConnection();
$this->_classMetadata = $classMetadata;
}
/**
* Inserts an entity.
*
* @param object $entity The entity to insert.
* @return mixed
*/
public function insert($entity)
{
$insertData = array();
$this->_prepareData($entity, $insertData, true);
$this->_conn->insert($this->_classMetadata->getTableName(), $insertData);
$idGen = $this->_em->getIdGenerator($this->_classMetadata->getClassName());
if ($idGen->isPostInsertGenerator()) {
return $idGen->generate($entity);
}
return null;
}
/**
* Adds an entity to the queued inserts.
*
* @param object $entity
*/
public function addInsert($entity)
{
$insertData = array();
$this->_prepareData($entity, $insertData, true);
$this->_queuedInserts[] = $insertData;
}
/**
* Executes all queued inserts.
*/
public function executeInserts()
{
$tableName = $this->_classMetadata->getTableName();
$stmt = $this->_conn->prepare($this->_classMetadata->getInsertSql());
foreach ($this->_queuedInserts as $insertData) {
$stmt->execute(array_values($insertData));
}
}
/**
* Updates an entity.
*
* @param object $entity The entity to update.
*/
public function update($entity)
{
$updateData = array();
$this->_prepareData($entity, $updateData);
$id = array_combine($this->_classMetadata->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity));
$this->_conn->update($this->_classMetadata->getTableName(), $updateData, $id);
}
/**
* Deletes an entity.
*
* @param object $entity The entity to delete.
*/
public function delete($entity)
{
$id = array_combine(
$this->_classMetadata->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity)
);
$this->_conn->delete($this->_classMetadata->getTableName(), $id);
}
/**
*
* @return Doctrine\ORM\ClassMetadata
*/
public function getClassMetadata()
{
return $this->_classMetadata;
}
/**
* Gets the name of the class in the entity hierarchy that owns the field with
* the given name. The owning class is the one that defines the field.
*
* @param string $fieldName
* @return string
* @todo Move to ClassMetadata?
*/
public function getOwningClass($fieldName)
{
if ($this->_classMetadata->isInheritanceTypeNone()) {
return $this->_classMetadata;
} else {
$mapping = $this->_classMetadata->getFieldMapping($fieldName);
return $mapping['inherited'];
}
\Doctrine\Common\DoctrineException::updateMe("Unable to find defining class of field '$fieldName'.");
}
/**
* Callback that is invoked during the SQL construction process.
*/
public function getCustomJoins()
{
return array();
}
/**
* Callback that is invoked during the SQL construction process.
*/
public function getCustomFields()
{
return array();
}
/**
* Prepares the data of an entity for an insert/update operation.
*
* @param object $entity
* @param array $array
* @param boolean $isInsert
*/
protected function _prepareData($entity, array &$result, $isInsert = false)
{
foreach ($this->_em->getUnitOfWork()->getEntityChangeSet($entity) as $field => $change) {
$oldVal = $change[0];
$newVal = $change[1];
$type = $this->_classMetadata->getTypeOfField($field);
$columnName = $this->_classMetadata->getColumnName($field);
if ($this->_classMetadata->hasAssociation($field)) {
$assocMapping = $this->_classMetadata->getAssociationMapping($field);
if ( ! $assocMapping->isOneToOne() || $assocMapping->isInverseSide()) {
continue;
}
foreach ($assocMapping->getSourceToTargetKeyColumns() as $sourceColumn => $targetColumn) {
$otherClass = $this->_em->getClassMetadata($assocMapping->getTargetEntityName());
if (is_null($newVal)) {
$result[$sourceColumn] = null;
} else {
$result[$sourceColumn] = $otherClass->getReflectionProperty(
$otherClass->getFieldName($targetColumn))->getValue($newVal);
}
}
} else if (is_null($newVal)) {
$result[$columnName] = null;
} else {
$result[$columnName] = $type->convertToDatabaseValue($newVal, $this->_conn->getDatabasePlatform());
}
}
// Populate the discriminator column on insert in JOINED & SINGLE_TABLE inheritance
if ($isInsert && ($this->_classMetadata->isInheritanceTypeJoined() ||
$this->_classMetadata->isInheritanceTypeSingleTable())) {
$discColumn = $this->_classMetadata->getDiscriminatorColumn();
$discMap = $this->_classMetadata->getDiscriminatorMap();
$result[$discColumn['name']] = array_search($this->_entityName, $discMap);
}
}
}