public function testAcceptanceSynchronization() { $synchronizer = CollectionSynchronizer::createFor('Psc\\Doctrine\\TestEntities\\Article', 'tags', $this->getDoctrinePackage()); $toCollection = $this->helpConvertToEntities(); $article = $this->findArticle(); $synchronizer->init($article); $synchronizer->process($article->getTags(), $toCollection); $this->em->flush(); $this->em->clear(); $this->assertSyncResult(); }
/** * Synchronisiert Collections in einem Property des Entities * * Initialisiert einen CollectionSynchronizer, um das $entity->$field vom $type zu $toCollection zu synchronisieren */ public function synchronizeCollectionNormal($entity, $field, $toCollection, Type $type) { if (!$this->dc) { throw new \RuntimeException('DCPackage muss für synchronizeCollectionNormal gesetzt sein'); } $entityMeta = $this->dc->getEntityMeta($entity->getEntityName()); $entityClassMeta = $entityMeta->getClassMetadata(); $entityAssoc = $entityClassMeta->getAssociationMapping($field); $fromCollection = $entity->callGetter($field); if (!$entityClassMeta->isCollectionValuedAssociation($field)) { throw new \RuntimeException('Das Property ' . $entity->getEntityName() . '->' . $field . ' ist keine Collection und kann somit nicht synchronisiert werden'); } $adder = $remover = NULL; if ($entityClassMeta->isAssociationInverseSide($field)) { $otherMeta = $this->dc->getEntityMeta($entityAssoc['targetEntity']); $otherMetaClass = $otherMeta->getClassMetadata(); $otherProperty = $entityAssoc['mappedBy']; $otherIsCollection = $otherMetaClass->isCollectionValuedAssociation($otherProperty); $addOrSet = $otherIsCollection ? 'add' . ucfirst($otherProperty) : 'set' . ucfirst($otherProperty); // da der CollectionSynchronizer nur die owningSide automatisch pflegt, injecten wir hier den adder und remover um die inverse Side zu pflegen $adder = function ($entity, $toEntity, $repository) use($addOrSet) { $repository->persist($toEntity); $toEntity->{$addOrSet}($entity); }; if ($otherIsCollection) { $remove = 'remove' . ucfirst($otherProperty); $remover = function ($entity, $toEntity, $repository) use($remove) { $repository->persist($toEntity); $toEntity->{$remove}($entity); }; } else { $set = 'set' . ucfirst($otherProperty); $remover = function ($entity, $toEntity, $repository) use($set) { $repository->persist($toEntity); $toEntity->{$set}(NULL); }; } } $synchronizer = CollectionSynchronizer::createFor($entity->getEntityName(), $field, $this->dc); $synchronizer->init($entity, $adder, $remover); return $synchronizer->process($fromCollection, $toCollection); }