/**
  * Returns the reference representation to be stored in mongodb or null if not applicable.
  *
  * @param ClassMetadata $class
  * @param Document $doc
  * @return array|null
  */
 private function _prepareDocReference(ClassMetadata $class, $doc)
 {
     if (!is_object($doc)) {
         return $doc;
     }
     $id = $this->_uow->getDocumentIdentifier($doc);
     $ref = array('$ref' => $class->getCollection(), '$id' => $id, '$db' => $class->getDB());
     return $ref;
 }
 /**
  * Returns a DBRef array for the supplied document.
  *
  * @param mixed $document A document object
  * @param array $referenceMapping Mapping for the field the references the document
  *
  * @return array A DBRef array
  */
 public function createDBRef($document, array $referenceMapping = null)
 {
     $class = $this->getClassMetadata(get_class($document));
     $id = $this->unitOfWork->getDocumentIdentifier($document);
     $dbRef = array($this->cmd . 'ref' => $class->getCollection(), $this->cmd . 'id' => $class->getDatabaseIdentifierValue($id), $this->cmd . 'db' => $class->getDatabase());
     // add a discriminator value if the referenced document is not mapped explicitely to a targetDocument
     if ($referenceMapping && !isset($referenceMapping['targetDocument'])) {
         $discriminatorField = isset($referenceMapping['discriminatorField']) ? $referenceMapping['discriminatorField'] : '_doctrine_class_name';
         $discriminatorValue = isset($referenceMapping['discriminatorMap']) ? array_search($class->getName(), $referenceMapping['discriminatorMap']) : $class->getName();
         $dbRef[$discriminatorField] = $discriminatorValue;
     }
     return $dbRef;
 }
Esempio n. 3
0
 /**
  * Returns a DBRef array for the supplied document.
  *
  * @param mixed $document A document object
  * @param array $referenceMapping Mapping for the field the references the document
  *
  * @return array A DBRef array
  */
 public function createDBRef($document, array $referenceMapping = null)
 {
     if (!is_object($document)) {
         throw new \InvalidArgumentException('Cannot create a DBRef, the document is not an object');
     }
     $className = get_class($document);
     $class = $this->getClassMetadata($className);
     $id = $this->unitOfWork->getDocumentIdentifier($document);
     $dbRef = array($this->cmd . 'ref' => $class->getCollection(), $this->cmd . 'id' => $class->getDatabaseIdentifierValue($id), $this->cmd . 'db' => $this->getDocumentDatabase($className)->getName());
     if ($class->discriminatorField) {
         $dbRef[$class->discriminatorField['name']] = $class->discriminatorValue;
     }
     // add a discriminator value if the referenced document is not mapped explicitely to a targetDocument
     if ($referenceMapping && !isset($referenceMapping['targetDocument'])) {
         $discriminatorField = isset($referenceMapping['discriminatorField']) ? $referenceMapping['discriminatorField'] : '_doctrine_class_name';
         $discriminatorValue = isset($referenceMapping['discriminatorMap']) ? array_search($class->getName(), $referenceMapping['discriminatorMap']) : $class->getName();
         $dbRef[$discriminatorField] = $discriminatorValue;
     }
     return $dbRef;
 }