Beispiel #1
0
 /**
  * Loads/creates a model from a persistence layer Record.
  *
  * @param   string  $typeKey    The model type.
  * @param   Record  $record     The persistence layer record.
  * @return  Model
  */
 protected function loadModel($typeKey, Record $record)
 {
     $this->mf->validateResourceTypes($typeKey, $record->getType());
     // Must use the type from the record to cover polymorphic models.
     $metadata = $this->getMetadataForType($record->getType());
     $model = new Model($metadata, $record->getId(), $this, $record);
     $model->getState()->setLoaded();
     $this->dispatchLifecycleEvent(Events::postLoad, $model);
     $this->cache->push($model);
     return $model;
 }
Beispiel #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;
 }