Doctrine\ODM\MongoDB\UnitOfWork::registerManaged PHP Method

registerManaged() public method

TODO: This method assumes that $id is a valid PHP identifier for the document class. If the class expects its database identifier to be a MongoId, and an incompatible $id is registered (e.g. an integer), the document identifiers map will become inconsistent with the identity map. In the future, we may want to round-trip $id through a PHP and database conversion and throw an exception if it's inconsistent.
public registerManaged ( object $document, array $id, array $data )
$document object The document.
$id array The identifier values.
$data array The original document data.
    public function registerManaged($document, $id, array $data)
    {
        $oid = spl_object_hash($document);
        $class = $this->dm->getClassMetadata(get_class($document));
        if (!$class->identifier || $id === null) {
            $this->documentIdentifiers[$oid] = $oid;
        } else {
            $this->documentIdentifiers[$oid] = $class->getPHPIdentifierValue($id);
        }
        $this->documentStates[$oid] = self::STATE_MANAGED;
        $this->originalDocumentData[$oid] = $data;
        $this->addToIdentityMap($document);
    }

Usage Example

 /**
  * Lood document by its identifier.
  *
  * @param string $id
  * @return object|null
  */
 public function loadById($id)
 {
     $result = $this->_collection->findOne(array('_id' => $this->_class->getDatabaseIdentifierValue($id)));
     if ($result !== null) {
         $document = $this->_uow->getOrCreateDocument($this->_documentName, $result);
         $this->_uow->registerManaged($document, $this->_class->getPHPIdentifierValue($result['_id']), $result);
         return $document;
     }
     return null;
 }
All Usage Examples Of Doctrine\ODM\MongoDB\UnitOfWork::registerManaged
UnitOfWork