private function fixPersistentCollectionOwnership(PersistentCollection $coll, $document, $class, $propName) { $owner = $coll->getOwner(); if ($owner === null) { // cloned $coll->setOwner($document, $class->fieldMappings[$propName]); } elseif ($owner !== $document) { // no clone, we have to fix if (!$coll->isInitialized()) { $coll->initialize(); // we have to do this otherwise the cols share state } $newValue = clone $coll; $newValue->setOwner($document, $class->fieldMappings[$propName]); $class->reflFields[$propName]->setValue($document, $newValue); return $newValue; } return $coll; }
/** * {@inheritdoc} */ public function initialize() { $this->collection->initialize(); }
/** * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: * 1) sets owner if it was cloned * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData * 3) NOP if state is OK * Returned collection should be used from now on (only important with 2nd point) * * @param PersistentCollection $coll * @param object $document * @param ClassMetadata $class * @param string $propName * @return PersistentCollection */ private function fixPersistentCollectionOwnership(PersistentCollection $coll, $document, ClassMetadata $class, $propName) { $owner = $coll->getOwner(); if ($owner === null) { // cloned $coll->setOwner($document, $class->fieldMappings[$propName]); } elseif ($owner !== $document) { // no clone, we have to fix if (!$coll->isInitialized()) { $coll->initialize(); // we have to do this otherwise the cols share state } $newValue = clone $coll; $newValue->setOwner($document, $class->fieldMappings[$propName]); $class->reflFields[$propName]->setValue($document, $newValue); if ($this->isScheduledForUpdate($document)) { // @todo following line should be superfluous once collections are stored in change sets $this->setOriginalDocumentProperty(spl_object_hash($document), $propName, $newValue); } return $newValue; } return $coll; }
/** * Updates a PersistentCollection instance deleting removed rows and * inserting new rows. * * @param PersistentCollection $coll * @param array $options */ public function update(PersistentCollection $coll, array $options) { $mapping = $coll->getMapping(); if ($mapping['isInverseSide']) { return; // ignore inverse side } switch ($mapping['strategy']) { case 'set': case 'setArray': $coll->initialize(); $this->setCollection($coll, $options); break; case 'addToSet': case 'pushAll': $coll->initialize(); $this->deleteElements($coll, $options); $this->insertElements($coll, $options); break; default: throw new \UnexpectedValueException('Unsupported collection strategy: ' . $mapping['strategy']); } }