public function createReference(object $document, array $referenceMapping)
    {
        $class = $this->getClassMetadata(get_class($document));
        $id    = $this->unitOfWork->getDocumentIdentifier($document);

        if ($id === null) {
            throw new RuntimeException(
                sprintf('Cannot create a DBRef for class %s without an identifier. Have you forgotten to persist/merge the document first?', $class->name)
            );
        }

        $storeAs = $referenceMapping['storeAs'] ?? null;
        switch ($storeAs) {
            case ClassMetadata::REFERENCE_STORE_AS_ID:
                if ($class->inheritanceType === ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION) {
                    throw MappingException::simpleReferenceMustNotTargetDiscriminatedDocument($referenceMapping['targetDocument']);
                }

                return $class->getDatabaseIdentifierValue($id);
                break;

            case ClassMetadata::REFERENCE_STORE_AS_REF:
                $reference = ['id' => $class->getDatabaseIdentifierValue($id)];
                break;

            case ClassMetadata::REFERENCE_STORE_AS_DB_REF:
                $reference = [
                    '$ref' => $class->getCollection(),
                    '$id'  => $class->getDatabaseIdentifierValue($id),
                ];
                break;

            case ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB:
                $reference = [
                    '$ref' => $class->getCollection(),
                    '$id'  => $class->getDatabaseIdentifierValue($id),
                    '$db'  => $this->getDocumentDatabase($class->name)->getDatabaseName(),
                ];
                break;

            default:
                throw new InvalidArgumentException(sprintf('Reference type %s is invalid.', $storeAs));
        }

        return $reference + $this->getDiscriminatorData($referenceMapping, $class);
    }
public function createReference(object $document, array $referenceMapping)
    {
        $class = $this->getClassMetadata(get_class($document));
        $id    = $this->unitOfWork->getDocumentIdentifier($document);

        if ($id === null) {
            throw new RuntimeException(
                sprintf('Cannot create a DBRef for class %s without an identifier. Have you forgotten to persist/merge the document first?', $class->name)
            );
        }

        $storeAs = $referenceMapping['storeAs'] ?? null;
        switch ($storeAs) {
            case ClassMetadata::REFERENCE_STORE_AS_ID:
                if ($class->inheritanceType === ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION) {
                    throw MappingException::simpleReferenceMustNotTargetDiscriminatedDocument($referenceMapping['targetDocument']);
                }

                return $class->getDatabaseIdentifierValue($id);
                break;

            case ClassMetadata::REFERENCE_STORE_AS_REF:
                $reference = ['id' => $class->getDatabaseIdentifierValue($id)];
                break;

            case ClassMetadata::REFERENCE_STORE_AS_DB_REF:
                $reference = [
                    '$ref' => $class->getCollection(),
                    '$id'  => $class->getDatabaseIdentifierValue($id),
                ];
                break;

            case ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB:
                $reference = [
                    '$ref' => $class->getCollection(),
                    '$id'  => $class->getDatabaseIdentifierValue($id),
                    '$db'  => $this->getDocumentDatabase($class->name)->getDatabaseName(),
                ];
                break;

            default:
                throw new InvalidArgumentException(sprintf('Reference type %s is invalid.', $storeAs));
        }

        return $reference + $this->getDiscriminatorData($referenceMapping, $class);
    }
public function createReference ( object $ document , array $ referenceMapping ) { $ class = $ this -> getClassMetadata ( get_class ( $ document ) ) ; $ id = $ this -> unitOfWork -> getDocumentIdentifier ( $ document ) ; if ( $ id === null ) { throw new RuntimeException ( sprintf ( 'Cannot create a DBRef for class %s without an identifier. Have you forgotten to persist/merge the document first?' , $ class -> name ) ) ; } $ storeAs = $ referenceMapping [ 'storeAs' ] ?? null ; switch ( $ storeAs ) { case ClassMetadata :: REFERENCE_STORE_AS_ID : if ( $ class -> inheritanceType === ClassMetadata :: INHERITANCE_TYPE_SINGLE_COLLECTION ) { throw MappingException :: simpleReferenceMustNotTargetDiscriminatedDocument ( $ referenceMapping [ 'targetDocument' ] ) ; } return $ class -> getDatabaseIdentifierValue ( $ id ) ; break ; case ClassMetadata :: REFERENCE_STORE_AS_REF : $ reference = [ 'id' => $ class -> getDatabaseIdentifierValue ( $ id ) ] ; break ; case ClassMetadata :: REFERENCE_STORE_AS_DB_REF : $ reference = [ '$ref' => $ class -> getCollection ( ) , '$id' => $ class -> getDatabaseIdentifierValue ( $ id ) , ] ; break ; case ClassMetadata :: REFERENCE_STORE_AS_DB_REF_WITH_DB : $ reference = [ '$ref' => $ class -> getCollection ( ) , '$id' => $ class -> getDatabaseIdentifierValue ( $ id ) , '$db' => $ this -> getDocumentDatabase ( $ class -> name ) -> getDatabaseName ( ) , ] ; break ; default : throw new InvalidArgumentException ( sprintf ( 'Reference type %s is invalid.' , $ storeAs ) ) ; } return $ reference + $ this -> getDiscriminatorData ( $ referenceMapping , $ class ) ; }
Returns a reference to the supplied document.

@return mixed The reference for the document in question, according to the desired mapping

@throws MappingException
@throws RuntimeException
Returns a reference to the supplied document .