Doctrine\ODM\MongoDB\PersistentCollection::initialize PHP Method

initialize() public method

Initializes the collection by loading its contents from the database if the collection is not yet initialized.
public initialize ( )
    public function initialize()
    {
        if ( ! $this->initialized && $this->mapping) {
            if ($this->isDirty) {
                // Has NEW objects added through add(). Remember them.
                $newObjects = $this->coll->toArray();
            }
            $this->coll->clear();
            $this->dm->getUnitOfWork()->loadCollection($this);
            $this->takeSnapshot();
            // Reattach NEW objects added through add(), if any.
            if (isset($newObjects)) {
                foreach ($newObjects as $obj) {
                    $this->coll->add($obj);
                }
                $this->isDirty = true;
            }
            $this->references = array();
            $this->initialized = true;
        }
    }

Usage Example

Example #1
0
 private function fixPersistentCollectionOwnership(PersistentCollection $coll, $document, $class, $propName)
 {
     $owner = $coll->getOwner();
     if ($owner === null) {
         // cloned
         $coll->setOwner($document, $class->fieldMappings[$propName]);
     } elseif ($owner !== $document) {
         // no clone, we have to fix
         if (!$coll->isInitialized()) {
             $coll->initialize();
             // we have to do this otherwise the cols share state
         }
         $newValue = clone $coll;
         $newValue->setOwner($document, $class->fieldMappings[$propName]);
         $class->reflFields[$propName]->setValue($document, $newValue);
         return $newValue;
     }
     return $coll;
 }
All Usage Examples Of Doctrine\ODM\MongoDB\PersistentCollection::initialize