1
0
mirror of synced 2025-01-30 20:11:49 +03:00

[2.0][DDC-145] Fixed.

This commit is contained in:
romanb 2009-11-13 19:34:10 +00:00
parent 94d771172e
commit c3ef019549
2 changed files with 62 additions and 22 deletions

View File

@ -629,6 +629,11 @@ class UnitOfWork implements PropertyChangedListener
if ( ! isset($this->_entityStates[$oid]) || $this->_entityStates[$oid] != self::STATE_MANAGED) { if ( ! isset($this->_entityStates[$oid]) || $this->_entityStates[$oid] != self::STATE_MANAGED) {
throw new \InvalidArgumentException('Entity must be managed.'); throw new \InvalidArgumentException('Entity must be managed.');
} }
/* TODO: Just return if changetracking policy is NOTIFY?
if ($class->isChangeTrackingNotify()) {
return;
}*/
if ( ! $class->isInheritanceTypeNone()) { if ( ! $class->isInheritanceTypeNone()) {
$class = $this->_em->getClassMetadata(get_class($entity)); $class = $this->_em->getClassMetadata(get_class($entity));
@ -690,7 +695,7 @@ class UnitOfWork implements PropertyChangedListener
$postInsertIds = $persister->executeInserts(); $postInsertIds = $persister->executeInserts();
if ($postInsertIds) { if ($postInsertIds) {
// Persister returned a post-insert IDs // Persister returned post-insert IDs
foreach ($postInsertIds as $id => $entity) { foreach ($postInsertIds as $id => $entity) {
$oid = spl_object_hash($entity); $oid = spl_object_hash($entity);
$idField = $class->identifier[0]; $idField = $class->identifier[0];
@ -1477,12 +1482,16 @@ class UnitOfWork implements PropertyChangedListener
private function _cascadeRefresh($entity, array &$visited) private function _cascadeRefresh($entity, array &$visited)
{ {
$class = $this->_em->getClassMetadata(get_class($entity)); $class = $this->_em->getClassMetadata(get_class($entity));
foreach ($class->associationMappings as $assocMapping) { foreach ($class->associationMappings as $assoc) {
if ( ! $assocMapping->isCascadeRefresh) { if ( ! $assoc->isCascadeRefresh) {
continue; continue;
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); $relatedEntities = $class->reflFields[$assoc->sourceFieldName]->getValue($entity);
if ($relatedEntities instanceof Collection) { if ($relatedEntities instanceof Collection) {
if ($relatedEntities instanceof PersistentCollection) {
// Unwrap so that foreach() does not initialize
$relatedEntities = $relatedEntities->unwrap();
}
foreach ($relatedEntities as $relatedEntity) { foreach ($relatedEntities as $relatedEntity) {
$this->_doRefresh($relatedEntity, $visited); $this->_doRefresh($relatedEntity, $visited);
} }
@ -1501,11 +1510,11 @@ class UnitOfWork implements PropertyChangedListener
private function _cascadeDetach($entity, array &$visited) private function _cascadeDetach($entity, array &$visited)
{ {
$class = $this->_em->getClassMetadata(get_class($entity)); $class = $this->_em->getClassMetadata(get_class($entity));
foreach ($class->associationMappings as $assocMapping) { foreach ($class->associationMappings as $assoc) {
if ( ! $assocMapping->isCascadeDetach) { if ( ! $assoc->isCascadeDetach) {
continue; continue;
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); $relatedEntities = $class->reflFields[$assoc->sourceFieldName]->getValue($entity);
if ($relatedEntities instanceof Collection) { if ($relatedEntities instanceof Collection) {
if ($relatedEntities instanceof PersistentCollection) { if ($relatedEntities instanceof PersistentCollection) {
// Unwrap so that foreach() does not initialize // Unwrap so that foreach() does not initialize
@ -1530,21 +1539,21 @@ class UnitOfWork implements PropertyChangedListener
private function _cascadeMerge($entity, $managedCopy, array &$visited) private function _cascadeMerge($entity, $managedCopy, array &$visited)
{ {
$class = $this->_em->getClassMetadata(get_class($entity)); $class = $this->_em->getClassMetadata(get_class($entity));
foreach ($class->associationMappings as $assocMapping) { foreach ($class->associationMappings as $assoc) {
if ( ! $assocMapping->isCascadeMerge) { if ( ! $assoc->isCascadeMerge) {
continue; continue;
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); $relatedEntities = $class->reflFields[$assoc->sourceFieldName]->getValue($entity);
if ($relatedEntities instanceof Collection) { if ($relatedEntities instanceof Collection) {
if ($relatedEntities instanceof PersistentCollection) { if ($relatedEntities instanceof PersistentCollection) {
// Unwrap so that foreach() does not initialize // Unwrap so that foreach() does not initialize
$relatedEntities = $relatedEntities->unwrap(); $relatedEntities = $relatedEntities->unwrap();
} }
foreach ($relatedEntities as $relatedEntity) { foreach ($relatedEntities as $relatedEntity) {
$this->_doMerge($relatedEntity, $visited, $managedCopy, $assocMapping); $this->_doMerge($relatedEntity, $visited, $managedCopy, $assoc);
} }
} else if ($relatedEntities !== null) { } else if ($relatedEntities !== null) {
$this->_doMerge($relatedEntities, $visited, $managedCopy, $assocMapping); $this->_doMerge($relatedEntities, $visited, $managedCopy, $assoc);
} }
} }
} }
@ -1559,11 +1568,11 @@ class UnitOfWork implements PropertyChangedListener
private function _cascadePersist($entity, array &$visited) private function _cascadePersist($entity, array &$visited)
{ {
$class = $this->_em->getClassMetadata(get_class($entity)); $class = $this->_em->getClassMetadata(get_class($entity));
foreach ($class->associationMappings as $assocMapping) { foreach ($class->associationMappings as $assoc) {
if ( ! $assocMapping->isCascadePersist) { if ( ! $assoc->isCascadePersist) {
continue; continue;
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); $relatedEntities = $class->reflFields[$assoc->sourceFieldName]->getValue($entity);
if (($relatedEntities instanceof Collection || is_array($relatedEntities))) { if (($relatedEntities instanceof Collection || is_array($relatedEntities))) {
if ($relatedEntities instanceof PersistentCollection) { if ($relatedEntities instanceof PersistentCollection) {
// Unwrap so that foreach() does not initialize // Unwrap so that foreach() does not initialize
@ -1587,16 +1596,13 @@ class UnitOfWork implements PropertyChangedListener
private function _cascadeRemove($entity, array &$visited) private function _cascadeRemove($entity, array &$visited)
{ {
$class = $this->_em->getClassMetadata(get_class($entity)); $class = $this->_em->getClassMetadata(get_class($entity));
foreach ($class->associationMappings as $assocMapping) { foreach ($class->associationMappings as $assoc) {
if ( ! $assocMapping->isCascadeRemove) { if ( ! $assoc->isCascadeRemove) {
continue; continue;
} }
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); $relatedEntities = $class->reflFields[$assoc->sourceFieldName]->getValue($entity);
if ($relatedEntities instanceof Collection || is_array($relatedEntities)) { if ($relatedEntities instanceof Collection || is_array($relatedEntities)) {
if ($relatedEntities instanceof PersistentCollection) { // If its a PersistentCollection initialization is intended! No unwrap!
// Unwrap so that foreach() does not initialize
$relatedEntities = $relatedEntities->unwrap();
}
foreach ($relatedEntities as $relatedEntity) { foreach ($relatedEntities as $relatedEntity) {
$this->_doRemove($relatedEntity, $visited); $this->_doRemove($relatedEntity, $visited);
} }

View File

@ -475,4 +475,38 @@ class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals('Berlin', $gblanco->getAddress()->getCity()); $this->assertEquals('Berlin', $gblanco->getAddress()->getCity());
} }
public function testOneToManyCascadeRemove()
{
$user = new CmsUser;
$user->name = 'Guilherme';
$user->username = 'gblanco';
$user->status = 'developer';
for ($i=0; $i<3; ++$i) {
$phone = new CmsPhonenumber;
$phone->phonenumber = 100 + $i;
$user->addPhonenumber($phone);
}
$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
$gblanco = $query->getSingleResult();
$this->_em->remove($gblanco);
$this->_em->flush();
$this->_em->clear();
$this->assertEquals(0, $this->_em->createQuery(
"select count(p.phonenumber) from Doctrine\Tests\Models\CMS\CmsPhonenumber p")
->getSingleScalarResult());
$this->assertEquals(0, $this->_em->createQuery(
"select count(u.id) from Doctrine\Tests\Models\CMS\CmsUser u")
->getSingleScalarResult());
}
} }