Second round of refactorings on BasicEntityPersister
This commit is contained in:
parent
04e1838c92
commit
827153624c
@ -413,31 +413,32 @@ class BasicEntityPersister
|
|||||||
$identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
$identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
|
||||||
|
|
||||||
foreach ($this->class->identifier as $idField) {
|
foreach ($this->class->identifier as $idField) {
|
||||||
if (isset($this->class->associationMappings[$idField])) {
|
if ( ! isset($this->class->associationMappings[$idField])) {
|
||||||
|
|
||||||
$params[] = $identifier[$idField];
|
$params[] = $identifier[$idField];
|
||||||
$where[] = $this->class->associationMappings[$idField]['joinColumns'][0]['name'];
|
$types[] = $this->class->fieldMappings[$idField]['type'];
|
||||||
$targetMapping = $this->em->getClassMetadata($this->class->associationMappings[$idField]['targetEntity']);
|
$where[] = $this->quoteStrategy->getColumnName($idField, $this->class, $this->platform);
|
||||||
|
|
||||||
switch (true) {
|
|
||||||
case (isset($targetMapping->fieldMappings[$targetMapping->identifier[0]])):
|
|
||||||
$types[] = $targetMapping->fieldMappings[$targetMapping->identifier[0]]['type'];
|
|
||||||
break;
|
|
||||||
|
|
||||||
case (isset($targetMapping->associationMappings[$targetMapping->identifier[0]])):
|
|
||||||
$types[] = $targetMapping->associationMappings[$targetMapping->identifier[0]]['type'];
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw ORMException::unrecognizedField($targetMapping->identifier[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$params[] = $identifier[$idField];
|
$params[] = $identifier[$idField];
|
||||||
$types[] = $this->class->fieldMappings[$idField]['type'];
|
$where[] = $this->class->associationMappings[$idField]['joinColumns'][0]['name'];
|
||||||
$where[] = $this->quoteStrategy->getColumnName($idField, $this->class, $this->platform);
|
$targetMapping = $this->em->getClassMetadata($this->class->associationMappings[$idField]['targetEntity']);
|
||||||
|
|
||||||
|
switch (true) {
|
||||||
|
case (isset($targetMapping->fieldMappings[$targetMapping->identifier[0]])):
|
||||||
|
$types[] = $targetMapping->fieldMappings[$targetMapping->identifier[0]]['type'];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case (isset($targetMapping->associationMappings[$targetMapping->identifier[0]])):
|
||||||
|
$types[] = $targetMapping->associationMappings[$targetMapping->identifier[0]]['type'];
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw ORMException::unrecognizedField($targetMapping->identifier[0]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($versioned) {
|
if ($versioned) {
|
||||||
@ -586,59 +587,68 @@ class BasicEntityPersister
|
|||||||
|
|
||||||
$newVal = $change[1];
|
$newVal = $change[1];
|
||||||
|
|
||||||
if (isset($this->class->associationMappings[$field])) {
|
if ( ! isset($this->class->associationMappings[$field])) {
|
||||||
$assoc = $this->class->associationMappings[$field];
|
|
||||||
|
|
||||||
// Only owning side of x-1 associations can have a FK column.
|
$columnName = $this->class->columnNames[$field];
|
||||||
if ( ! $assoc['isOwningSide'] || ! ($assoc['type'] & ClassMetadata::TO_ONE)) {
|
$this->columnTypes[$columnName] = $this->class->fieldMappings[$field]['type'];
|
||||||
continue;
|
$result[$this->getOwningTable($field)][$columnName] = $newVal;
|
||||||
}
|
|
||||||
|
|
||||||
if ($newVal !== null) {
|
|
||||||
$oid = spl_object_hash($newVal);
|
|
||||||
|
|
||||||
if (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) {
|
|
||||||
// The associated entity $newVal is not yet persisted, so we must
|
|
||||||
// set $newVal = null, in order to insert a null value and schedule an
|
|
||||||
// extra update on the UnitOfWork.
|
|
||||||
$uow->scheduleExtraUpdate($entity, array(
|
|
||||||
$field => array(null, $newVal)
|
|
||||||
));
|
|
||||||
$newVal = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($newVal !== null) {
|
|
||||||
$newValId = $uow->getEntityIdentifier($newVal);
|
|
||||||
}
|
|
||||||
|
|
||||||
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
|
|
||||||
$owningTable = $this->getOwningTable($field);
|
|
||||||
|
|
||||||
foreach ($assoc['joinColumns'] as $joinColumn) {
|
|
||||||
$sourceColumn = $joinColumn['name'];
|
|
||||||
$targetColumn = $joinColumn['referencedColumnName'];
|
|
||||||
$quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform);
|
|
||||||
|
|
||||||
$this->quotedColumns[$sourceColumn] = $quotedColumn;
|
|
||||||
|
|
||||||
if ($newVal === null) {
|
|
||||||
$result[$owningTable][$sourceColumn] = null;
|
|
||||||
} else if ($targetClass->containsForeignIdentifier) {
|
|
||||||
$result[$owningTable][$sourceColumn] = $newValId[$targetClass->getFieldForColumn($targetColumn)];
|
|
||||||
} else {
|
|
||||||
$result[$owningTable][$sourceColumn] = $newValId[$targetClass->fieldNames[$targetColumn]];
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn);
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$columnName = $this->class->columnNames[$field];
|
$assoc = $this->class->associationMappings[$field];
|
||||||
$this->columnTypes[$columnName] = $this->class->fieldMappings[$field]['type'];
|
|
||||||
$result[$this->getOwningTable($field)][$columnName] = $newVal;
|
// Only owning side of x-1 associations can have a FK column.
|
||||||
|
if ( ! $assoc['isOwningSide'] || ! ($assoc['type'] & ClassMetadata::TO_ONE)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($newVal !== null) {
|
||||||
|
$oid = spl_object_hash($newVal);
|
||||||
|
|
||||||
|
if (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) {
|
||||||
|
// The associated entity $newVal is not yet persisted, so we must
|
||||||
|
// set $newVal = null, in order to insert a null value and schedule an
|
||||||
|
// extra update on the UnitOfWork.
|
||||||
|
$uow->scheduleExtraUpdate($entity, array(
|
||||||
|
$field => array(null, $newVal)
|
||||||
|
));
|
||||||
|
$newVal = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($newVal !== null) {
|
||||||
|
$newValId = $uow->getEntityIdentifier($newVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
|
||||||
|
$owningTable = $this->getOwningTable($field);
|
||||||
|
|
||||||
|
foreach ($assoc['joinColumns'] as $joinColumn) {
|
||||||
|
$sourceColumn = $joinColumn['name'];
|
||||||
|
$targetColumn = $joinColumn['referencedColumnName'];
|
||||||
|
$quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform);
|
||||||
|
|
||||||
|
$this->quotedColumns[$sourceColumn] = $quotedColumn;
|
||||||
|
$this->columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn);
|
||||||
|
|
||||||
|
switch (true) {
|
||||||
|
case $newVal === null:
|
||||||
|
$value = null;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case $targetClass->containsForeignIdentifier:
|
||||||
|
$value = $newValId[$targetClass->getFieldForColumn($targetColumn)];
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
$value = $newValId[$targetClass->fieldNames[$targetColumn]];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result[$owningTable][$sourceColumn] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
@ -747,31 +757,33 @@ class BasicEntityPersister
|
|||||||
if ($targetEntity !== null && $isInverseSingleValued) {
|
if ($targetEntity !== null && $isInverseSingleValued) {
|
||||||
$targetClass->reflFields[$assoc['inversedBy']]->setValue($targetEntity, $sourceEntity);
|
$targetClass->reflFields[$assoc['inversedBy']]->setValue($targetEntity, $sourceEntity);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$sourceClass = $this->em->getClassMetadata($assoc['sourceEntity']);
|
|
||||||
$owningAssoc = $targetClass->getAssociationMapping($assoc['mappedBy']);
|
|
||||||
|
|
||||||
// TRICKY: since the association is specular source and target are flipped
|
return $targetEntity;
|
||||||
foreach ($owningAssoc['targetToSourceKeyColumns'] as $sourceKeyColumn => $targetKeyColumn) {
|
}
|
||||||
if ( ! isset($sourceClass->fieldNames[$sourceKeyColumn])) {
|
|
||||||
throw MappingException::joinColumnMustPointToMappedField(
|
|
||||||
$sourceClass->name, $sourceKeyColumn
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// unset the old value and set the new sql aliased value here. By definition
|
$sourceClass = $this->em->getClassMetadata($assoc['sourceEntity']);
|
||||||
// unset($identifier[$targetKeyColumn] works here with how UnitOfWork::createEntity() calls this method.
|
$owningAssoc = $targetClass->getAssociationMapping($assoc['mappedBy']);
|
||||||
$identifier[$this->getSQLTableAlias($targetClass->name) . "." . $targetKeyColumn] =
|
|
||||||
$sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
|
|
||||||
|
|
||||||
unset($identifier[$targetKeyColumn]);
|
// TRICKY: since the association is specular source and target are flipped
|
||||||
|
foreach ($owningAssoc['targetToSourceKeyColumns'] as $sourceKeyColumn => $targetKeyColumn) {
|
||||||
|
if ( ! isset($sourceClass->fieldNames[$sourceKeyColumn])) {
|
||||||
|
throw MappingException::joinColumnMustPointToMappedField(
|
||||||
|
$sourceClass->name, $sourceKeyColumn
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$targetEntity = $this->load($identifier, null, $assoc);
|
// unset the old value and set the new sql aliased value here. By definition
|
||||||
|
// unset($identifier[$targetKeyColumn] works here with how UnitOfWork::createEntity() calls this method.
|
||||||
|
$identifier[$this->getSQLTableAlias($targetClass->name) . "." . $targetKeyColumn] =
|
||||||
|
$sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
|
||||||
|
|
||||||
if ($targetEntity !== null) {
|
unset($identifier[$targetKeyColumn]);
|
||||||
$targetClass->setFieldValue($targetEntity, $assoc['mappedBy'], $sourceEntity);
|
}
|
||||||
}
|
|
||||||
|
$targetEntity = $this->load($identifier, null, $assoc);
|
||||||
|
|
||||||
|
if ($targetEntity !== null) {
|
||||||
|
$targetClass->setFieldValue($targetEntity, $assoc['mappedBy'], $sourceEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $targetEntity;
|
return $targetEntity;
|
||||||
@ -974,7 +986,6 @@ class BasicEntityPersister
|
|||||||
|
|
||||||
foreach ($joinColumns as $joinColumn) {
|
foreach ($joinColumns as $joinColumn) {
|
||||||
|
|
||||||
$relationKeyColumn = $joinColumn['name'];
|
|
||||||
$sourceKeyColumn = $joinColumn['referencedColumnName'];
|
$sourceKeyColumn = $joinColumn['referencedColumnName'];
|
||||||
$quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
|
$quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user