Doctrine\ODM\MongoDB\Persisters\PersistenceBuilder::prepareEmbeddedDocValue PHP Method

prepareEmbeddedDocValue() public method

Prepares array of values to be stored in mongo to represent embedded object.
public prepareEmbeddedDocValue ( array $embeddedMapping, Document $embeddedDocument ) : array
$embeddedMapping array
$embeddedDocument Document
return array
    public function prepareEmbeddedDocValue(array $embeddedMapping, $embeddedDocument)
    {
        $className = get_class($embeddedDocument);
        $class = $this->dm->getClassMetadata($className);
        $embeddedDocumentValue = array();
        foreach ($class->fieldMappings as $mapping) {
            // Skip not saved fields
            if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) {
                continue;
            }

            $rawValue = $class->getFieldValue($embeddedDocument, $mapping['fieldName']);

            // Generate embedded document identifiers
            if ($class->isIdentifier($mapping['fieldName'])) {
                if ( ! $class->isIdGeneratorNone() && $rawValue === null) {
                    $rawValue = $class->idGenerator->generate($this->dm, $embeddedDocument);
                    $class->setIdentifierValue($embeddedDocument, $rawValue);
                }
                $embeddedDocumentValue['_id'] = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue);
                continue;
            }

            // Don't store null values unless nullable is specified
            if ($rawValue === null && $mapping['nullable'] === false) {
                continue;
            }
            $value = null;
            if (isset($mapping['embedded']) && $mapping['type'] == 'one') {
                $value = $this->prepareEmbeddedDocValue($mapping, $rawValue);
            } elseif (isset($mapping['embedded']) && $mapping['type'] == 'many') {
                // do nothing for embedded many
                // CollectionPersister will take care of this
            } elseif (isset($mapping['reference']) && $mapping['type'] === 'one') {
                $value = $this->prepareReferencedDocValue($mapping, $rawValue);
            } else {
                $value = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue);
            }
            if ($value === null && $mapping['nullable'] === false) {
                continue;
            }
            $embeddedDocumentValue[$mapping['name']] = $value;
        }

        // Store a discriminator value if the embedded document is not mapped explicitely to a targetDocument
        if ( ! isset($embeddedMapping['targetDocument'])) {
            $discriminatorField = isset($embeddedMapping['discriminatorField']) ? $embeddedMapping['discriminatorField'] : '_doctrine_class_name';
            $discriminatorValue = isset($embeddedMapping['discriminatorMap']) ? array_search($class->getName(), $embeddedMapping['discriminatorMap']) : $class->getName();
            $embeddedDocumentValue[$discriminatorField] = $discriminatorValue;
        }

        // Fix so that we can force empty embedded document to store itself as a hash instead of an array
        if (empty($embeddedDocumentValue)) {
            return (object) $embeddedDocumentValue;
        }
        return $embeddedDocumentValue;
    }

Usage Example

コード例 #1
0
 /**
  * Inserts new rows for a PersistentCollection instance.
  *
  * @param PersistentCollection $coll
  * @param array $options
  */
 private function insertRows(PersistentCollection $coll, array $options)
 {
     $mapping = $coll->getMapping();
     list($propertyPath, $parent) = $this->getPathAndParent($coll);
     if ($mapping['strategy'] === 'set') {
         $setData = array();
         foreach ($coll as $document) {
             if (isset($mapping['reference'])) {
                 $setData[] = $this->pb->prepareReferencedDocValue($mapping, $document);
             } else {
                 $setData[] = $this->pb->prepareEmbeddedDocValue($mapping, $document);
             }
         }
         $query = array($this->cmd.'set' => array($propertyPath => $setData));
         $this->executeQuery($parent, $query, $options);
     } else {
         $strategy = isset($mapping['strategy']) ? $mapping['strategy'] : 'pushAll';
         $insertDiff = $coll->getInsertDiff();
         if ($insertDiff) {
             $query = array($this->cmd.$strategy => array());
             foreach ($insertDiff as $key => $document) {
                 if (isset($mapping['reference'])) {
                     $query[$this->cmd.$strategy][$propertyPath][] = $this->pb->prepareReferencedDocValue($mapping, $document);
                 } else {
                     $query[$this->cmd.$strategy][$propertyPath][] = $this->pb->prepareEmbeddedDocValue($mapping, $document);
                 }
             }
             $this->executeQuery($parent, $query, $options);
         }
     }
 }