protected function debugCollection($collection, $label = NULL) { return \Psc\Doctrine\Helper::debugCollection($collection, "\n", $label); }
/** * Synchronisiert eine Collection eines Entities mit Entities in der Datenbank (deprecated) * * Diese Art der Synchroniserung ist deprecated, weil es mittlerweile ausgelagerte Funktionen dafür gibt * (siehe Psc\Doctrine\*Synchro*) * * also es werden für die bestehenden Objekte in $entity->$collectionProperty die objekte gelöscht * die nicht in newCollection drin sind und die, die in $newCollection drin sind aber nicht in * $entity->$collectionProperty gelöscht * * - Achtung(!): dies führt persist() auf den Entitys der Collection (also quasi andere Seite der Association) aus, wenn $this->entity nicht die Owning-Side der CollectionAssociation ist * - Dies führt persist für items in newCollection, die neu sind * * Die Basis für eine erfolgreiche Synchronization ist eine $newCollection die wirklich die gleichen Objekte enthält, für die Updates gemacht werden sollen. Also vorsicht mit serialisierten Einträgen und clones * * @param string $associationName der Name des Properties der Collection in $entity */ public function synchronizeCollection($associationName, $newCollection) { if (!is_string($associationName)) { throw new \Psc\Exception('string erwartet: ' . Code::varInfo($associationName)); } /* $mapping = $coll->getMapping(); $targetClass = $this->_em->getClassMetadata($mapping['targetEntity']); $sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']); $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); */ if ($this->entity instanceof \Psc\Doctrine\Object) { $entityClass = $this->entity->getDoctrineMetadata(); // Sound } elseif ($this->entity instanceof \Psc\Doctrine\Entity) { $entityClass = $this->em->getClassMetadata($this->entity->getEntityName()); } else { throw new \InvalidArgumentException('$this->entity ist von der Klasse: ' . Code::getClass($this->entity) . ' und diese ist unbekannt'); } $entityAssoc = $entityClass->getAssociationMapping($associationName); // speakers $owning = $entityAssoc['isOwningSide']; // true $targetClass = $this->em->getClassMetadata($entityAssoc['targetEntity']); // Metadata:Speaker if ($owning) { $entityProperty = $entityAssoc['inversedBy']; $collectionProperty = $entityAssoc['fieldName']; } else { $entityProperty = $entityAssoc['mappedBy']; $collectionProperty = $entityAssoc['fieldName']; } // entityProperty = sounds // collectionProperty = speakers $getter = Code::castGetter($collectionProperty); //getSpeakers $collection = $getter($this->entity); // $this->entity->getSpeakers() $collection = Code::castCollection($collection); $newCollection = Code::castCollection($newCollection); $this->log(sprintf("synchronizeCollection '%s'", $associationName)); $this->log('in DBCollection:'); $this->log(DoctrineHelper::debugCollection($collection)); $this->log(NULL); $this->log('in FormCollection:'); $this->log(DoctrineHelper::debugCollection($newCollection)); $this->log(NULL); $this->log('Processing:'); /* Wir synchronisieren hier auf der Owning Side oder Reverse Side jenachdem müssen wir auf der owning oder reverse side die add + remove funktionen aufrufen */ if ($owning) { $remove = 'remove' . ucfirst(Inflector::singular($collectionProperty)); // removeSpeaker $add = 'add' . ucfirst(Inflector::singular($collectionProperty)); // addSpeaker } else { $remove = 'remove' . ucfirst(Inflector::singular($entityProperty)); // removeSound $add = 'add' . ucfirst(Inflector::singular($entityProperty)); // addSound } $logThis = Code::getClassName($entityClass->getName()); // @TODO hier mit reflection prüfen //if (!->hasMethod($remove)) { // throw new \Psc\Exception('Es gibt keine '.$remove.' Methode im Entity: '.$owningEntity); //} //if (!$this->entity->hasMethod($add)) { // throw new \Psc\Exception('Es gibt keine '.$add.' Methode im Entity: '.$owningEntity); //} // foreach ($collection->deleteDiff($newCollection, ArrayCollection::COMPARE_OBJECTS) as $entity) { if ($owning) { $this->entity->{$remove}($entity); // $sound->removeSpeaker($speaker) } else { $this->em->persist($entity); // $speaker persist $entity->{$remove}($this->entity); // $speaker->removeSound($sound) } } foreach ($collection->insertDiff($newCollection, ArrayCollection::COMPARE_OBJECTS) as $entity) { if ($owning) { $this->entity->{$add}($entity); $this->em->persist($entity); } else { $entity->{$add}($this->entity); $this->em->persist($entity); } } }