/**
  * Loads this collection from the store.
  */
 protected function loadFromStore()
 {
     if (false === $this->isLoaded()) {
         // Loads collection from the database on iteration.
         $models = $this->store->loadCollection($this);
         $this->setModels($models);
         $this->loaded = true;
     }
 }
Esempio n. 2
0
 /**
  * Initializes the model and loads its attributes and relationships.
  *
  * @todo    Made public so collections can initialize models. Not sure if we want this??
  * @param   Record|null   $record     The db attributes and relationships to apply.
  * @return  self
  */
 public function initialize(Record $record = null)
 {
     $hasOne = [];
     $hasMany = [];
     $attributes = [];
     if (null !== $record) {
         $attributes = $this->applyDefaultAttrValues($attributes);
         foreach ($record->getProperties() as $key => $value) {
             if (true === $this->isAttribute($key)) {
                 // Load attribute.
                 $attributes[$key] = $this->convertAttributeValue($key, $value);
                 continue;
             }
             if (true === $this->isHasOne($key)) {
                 // Load hasOne relationship.
                 $hasOne[$key] = $this->store->loadProxyModel($value['type'], $value['id']);
                 continue;
             }
         }
     }
     foreach ($this->getMetadata()->getRelationships() as $key => $relMeta) {
         if (true === $relMeta->isOne()) {
             continue;
         }
         if (true === $relMeta->isInverse) {
             $hasMany[$key] = $this->store->createInverseCollection($relMeta, $this);
         } else {
             $references = null === $record || !isset($record->getProperties()[$key]) ? [] : $record->getProperties()[$key];
             $hasMany[$key] = $this->store->createCollection($relMeta, $references);
         }
     }
     $this->attributes = null === $this->attributes ? new Attributes($attributes) : $this->attributes->replace($attributes);
     $this->hasOneRelationships = null === $this->hasOneRelationships ? new Relationships\HasOne($hasOne) : $this->hasOneRelationships->replace($hasOne);
     $this->hasManyRelationships = null === $this->hasManyRelationships ? new Relationships\HasMany($hasMany) : $this->hasManyRelationships->replace($hasMany);
     $this->doDirtyCheck();
     return $this;
 }
Esempio n. 3
0
 /**
  * Processes raw MongoDB data an converts it into a standardized Record object.
  *
  * @param   EntityMetadata  $metadata
  * @param   array           $data
  * @param   Store           $store
  * @return  Record
  */
 protected function hydrateRecord(EntityMetadata $metadata, array $data, Store $store)
 {
     $identifier = $data[$this->getIdentifierKey()];
     unset($data[$this->getIdentifierKey()]);
     $type = $this->extractType($metadata, $data);
     unset($data[$this->getPolymorphicKey()]);
     $metadata = $store->getMetadataForType($type);
     foreach ($metadata->getRelationships() as $key => $relMeta) {
         if (!isset($data[$key])) {
             continue;
         }
         if (true === $relMeta->isMany() && !is_array($data[$key])) {
             throw PersisterException::badRequest(sprintf('Relationship key "%s" is a reference many. Expected record data type of array, "%s" found on model "%s" for identifier "%s"', $key, gettype($data[$key]), $type, $identifier));
         }
         $references = $relMeta->isOne() ? [$data[$key]] : $data[$key];
         $extracted = [];
         foreach ($references as $reference) {
             $extracted[] = $this->extractRelationship($relMeta, $reference);
         }
         $data[$key] = $relMeta->isOne() ? reset($extracted) : $extracted;
     }
     return new Record($type, $identifier, $data);
 }