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

prepareReferencedDocValue() public method

Returns the reference representation to be stored in mongodb or null if not applicable.
public prepareReferencedDocValue ( array $referenceMapping, Document $document ) : array | null
$referenceMapping array
$document Document
return array | null
    public function prepareReferencedDocValue(array $referenceMapping, $document)
    {
        $id = null;
        if (is_array($document)) {
            $className = $referenceMapping['targetDocument'];
        } else {
            $className = get_class($document);
            $id = $this->uow->getDocumentIdentifier($document);
        }
        $class = $this->dm->getClassMetadata($className);
        if (null !== $id) {
            $id = $class->getDatabaseIdentifierValue($id);
        }
        $ref = array(
            $this->cmd . 'ref' => $class->getCollection(),
            $this->cmd . 'id' => $id,
            $this->cmd . 'db' => $class->getDatabase()
        );

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

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);
         }
     }
 }