Beispiel #1
0
 /**
  * Build new instance from an eloquent model object.
  *
  * @param \Illuminate\Database\Eloquent\Model $eloquentModel
  * @return \ProAI\Datamapper\Support\Entity
  */
 public static function newFromEloquentObject(EloquentModel $eloquentModel)
 {
     $rc = new ReflectionClass(static::class);
     $entity = $rc->newInstanceWithoutConstructor();
     // get model data
     $dict = ['mapping' => $eloquentModel->getMapping(), 'attributes' => $eloquentModel->getAttributes(), 'relations' => $eloquentModel->getRelations()];
     // attributes
     foreach ($dict['mapping']['attributes'] as $attribute => $column) {
         $entity->{$attribute} = $dict['attributes'][$column];
     }
     // embeddeds
     foreach ($dict['mapping']['embeddeds'] as $name => $embedded) {
         $entity->{$name} = $embedded['class']::newFromEloquentObject($eloquentModel, $name);
     }
     // relations
     foreach ($dict['mapping']['relations'] as $name => $relation) {
         // set relation object
         if (!empty($dict['relations'][$name])) {
             $relationObject = $dict['relations'][$name]->toDatamapperObject();
         } elseif (in_array($relation['type'], $eloquentModel->manyRelations)) {
             $relationObject = new ProxyCollection();
         } else {
             $relationObject = new Proxy();
         }
         $entity->{$name} = $relationObject;
     }
     return $entity;
 }
 /**
  * Update relations.
  *
  * @param \ProAI\Datamapper\Eloquent\Model $eloquentModel
  * @param string $action
  * @return void
  */
 protected function updateRelations($eloquentModel, $action = 'insert')
 {
     $mapping = $eloquentModel->getMapping();
     $eloquentRelations = $eloquentModel->getRelations();
     foreach ($mapping['relations'] as $name => $relationMapping) {
         if (isset($eloquentRelations[$name])) {
             $this->updateRelation($eloquentModel, $name, $relationMapping, $action);
         }
     }
 }