Ejemplo n.º 1
0
 /**
  * Convert models to eloquent models.
  *
  * @param \ProAI\Datamapper\Support\Collection $object
  * @return \ProAI\Datamapper\Eloquent\Collection
  */
 public static function newFromEntity($entities)
 {
     $eloquentModels = new static();
     foreach ($entities as $name => $item) {
         $eloquentModels->put($name, Model::newFromEntity($item));
     }
     return $eloquentModels;
 }
Ejemplo n.º 2
0
 /**
  * Convert models to eloquent models.
  *
  * @param \ProAI\Datamapper\Support\Collection $entities
  * @param string $lastObjectId
  * @param \ProAI\Datamapper\Eloquent\Model $lastEloquentModel
  * @return \ProAI\Datamapper\Eloquent\Collection
  */
 public static function newFromDatamapperObject($entities, $lastObjectId, $lastEloquentModel)
 {
     $eloquentModels = new static();
     foreach ($entities as $name => $item) {
         if (spl_object_hash($item) == $lastObjectId) {
             $model = $lastEloquentModel;
         } else {
             $model = Model::newFromDatamapperObject($item, $lastObjectId, $lastEloquentModel);
         }
         $eloquentModels->put($name, $model);
     }
     return $eloquentModels;
 }
Ejemplo n.º 3
0
 /**
  * Convert an instance to an eloquent model object.
  *
  * @param string $lastObjectId
  * @param \ProAI\Datamapper\Eloquent\Model $lastEloquentModel
  * @return \ProAI\Datamapper\Eloquent\Model
  */
 public function toEloquentObject($lastObjectId, $lastEloquentModel)
 {
     $class = get_mapped_model(static::class);
     $eloquentModel = new $class();
     // get model data
     $dict = ['mapping' => $eloquentModel->getMapping(), 'attributes' => $eloquentModel->getAttributes(), 'relations' => $eloquentModel->getRelations()];
     // attributes
     foreach ($dict['mapping']['attributes'] as $attribute => $column) {
         if (!$eloquentModel->isAutomaticallyUpdatedDate($column)) {
             $eloquentModel->setAttribute($column, $this->{$attribute});
         }
     }
     // embeddeds
     foreach ($dict['mapping']['embeddeds'] as $name => $embedded) {
         $embeddedObject = $this->{$name};
         if (!empty($embeddedObject)) {
             $embeddedObject->toEloquentObject($eloquentModel, $name);
         }
     }
     // relations
     foreach ($dict['mapping']['relations'] as $name => $relation) {
         $relationObject = $this->{$name};
         if (!empty($relationObject) && !$relationObject instanceof \ProAI\Datamapper\Contracts\Proxy) {
             // set relation
             if ($relationObject instanceof \ProAI\Datamapper\Support\Collection) {
                 $value = EloquentCollection::newFromDatamapperObject($relationObject, $this, $eloquentModel);
             } elseif (spl_object_hash($relationObject) == $lastObjectId) {
                 $value = $lastEloquentModel;
             } else {
                 $value = EloquentModel::newFromDatamapperObject($relationObject, spl_object_hash($this), $eloquentModel);
             }
             $eloquentModel->setRelation($name, $value);
         }
     }
     return $eloquentModel;
 }
Ejemplo n.º 4
0
 /**
  * Delete an entity object.
  *
  * @param object $entity
  * @return \ProAI\Datamapper\Eloquent\Model
  */
 protected function getEloquentModel($entity, $exists = false)
 {
     if (empty($entity)) {
         throw new Exception('Object transfered to EntityManager is empty');
     }
     if (!is_object($entity)) {
         throw new Exception('Object transfered to EntityManager is not an object');
     }
     $eloquentModel = Model::newFromDatamapperObject($entity);
     $eloquentModel->exists = $exists;
     return $eloquentModel;
 }