コード例 #1
0
ファイル: AbstractModel.php プロジェクト: as3io/modlr
 /**
  * Initializes the model and loads its attributes and relationships.
  *
  * @todo    Made public so collections can initialize models. Not sure if we want this??
  * @param   array|null      $properties     The db properties to apply.
  * @return  self
  */
 public function initialize(array $properties = null)
 {
     $attributes = [];
     $embedOne = [];
     $embedMany = [];
     if (null !== $properties) {
         $attributes = $this->applyDefaultAttrValues($attributes);
         foreach ($properties as $key => $value) {
             if (true === $this->isAttribute($key)) {
                 // Load attribute.
                 $attributes[$key] = $this->convertAttributeValue($key, $value);
             } else {
                 if (true === $this->isEmbedHasOne($key) && is_array($value)) {
                     // Load embed one.
                     $embedOne[$key] = $this->getStore()->loadEmbed($this->getMetadata()->getEmbed($key)->embedMeta, $value);
                 }
             }
         }
     }
     foreach ($this->getMetadata()->getEmbeds() as $key => $embeddedPropMeta) {
         // Always load embedded collections, regardless if data is set.
         if (true === $embeddedPropMeta->isOne()) {
             continue;
         }
         $embeds = !isset($properties[$key]) ? [] : $properties[$key];
         $embedMany[$key] = $this->getStore()->createEmbedCollection($embeddedPropMeta, $embeds);
     }
     $this->attributes = null === $this->attributes ? new Attributes($attributes) : $this->attributes->replace($attributes);
     $this->hasOneEmbeds = null === $this->hasOneEmbeds ? new Embeds\HasOne($embedOne) : $this->hasOneEmbeds->replace($embedOne);
     $this->hasManyEmbeds = null === $this->hasManyEmbeds ? new Embeds\HasMany($embedMany) : $this->hasManyEmbeds->replace($embedMany);
     if (true === $this->getState()->is('new')) {
         // Ensure default values are applied to new models.
         $this->apply([]);
     }
     $this->doDirtyCheck();
     return $this;
 }
コード例 #2
0
ファイル: Model.php プロジェクト: actinoids/modlr-rest-odm
 /**
  * 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;
 }