Exemplo n.º 1
0
 /**
  * Create a new has many relationship instance.
  *
  * @param  \Analogue\ORM\Query  $query
  * @param  \Analogue\ORM\Entityl  $parent
  * @param  string  $name
  * @param  string  $table
  * @param  string  $foreignKey
  * @param  string  $otherKey
  * @param  string  $relationName
  * @param  bool   $inverse
  * @return void
  */
 public function __construct(Mapper $mapper, $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false)
 {
     $this->inverse = $inverse;
     $this->morphType = $name . '_type';
     $this->morphClass = $inverse ? $mapper->getEntityMap()->getClass() : get_class($parent);
     parent::__construct($mapper, $parent, $table, $foreignKey, $otherKey, $relationName);
 }
Exemplo n.º 2
0
 /**
  * 
  * @param \Analogue\ORM\Mappable $entity 
  * @param \Analogue\ORM\System\Mapper $mapper 
  */
 public function __construct(Mappable $entity, Mapper $mapper)
 {
     $this->entity = $entity;
     $this->mapper = $mapper;
     $this->entityMap = $mapper->getEntityMap();
     $this->keyName = $this->entityMap->getKeyName();
 }
Exemplo n.º 3
0
 public function __construct(Mapper $mapper, array $eagerLoads)
 {
     $this->mapper = $mapper;
     $this->entityMap = $mapper->getEntityMap();
     $this->eagerLoads = $eagerLoads;
     $this->lazyLoads = $this->prepareLazyLoading();
     $this->entityMap = $mapper->getEntityMap();
 }
Exemplo n.º 4
0
 public function __construct(Mappable $entity, Mapper $mapper, QueryAdapter $query)
 {
     $this->entity = $entity;
     $this->mapper = $mapper;
     $this->entityState = new StateChecker($entity, $mapper);
     $this->entityMap = $mapper->getEntityMap();
     $this->query = $query->from($this->entityMap->getTable());
 }
Exemplo n.º 5
0
 /**
  * Create a new Analogue Query Builder instance.
  *
  * @param \Analogue\ORM\Drivers\QueryAdapter  $query
  * @param \Analogue\ORM\System\Mapper $mapper
  * @return void
  */
 public function __construct(Mapper $mapper, DBAdapter $adapter)
 {
     $this->mapper = $mapper;
     $this->adapter = $adapter;
     $this->entityMap = $mapper->getEntityMap();
     // Specify the table to work on
     $this->query = $adapter->getQuery()->from($this->entityMap->getTable());
     $this->with($this->entityMap->getEagerloadedRelationships());
 }
Exemplo n.º 6
0
 /**
  * Return a new Mapper instance
  * 
  * @param  string       $entityClass 
  * @param  string       $entityMap
  * @return Mapper                     
  */
 public function make($entityClass, EntityMap $entityMap)
 {
     $driver = $entityMap->getDriver();
     $connection = $entityMap->getConnection();
     $adapter = $this->drivers->getAdapter($driver, $connection);
     $entityMap->setDateFormat($adapter->getDateFormat());
     $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager);
     // Fire Initializing Event
     $mapper->fireEvent('initializing', $mapper);
     $mapInitializer = new MapInitializer($entityMap);
     $mapInitializer->init();
     // Fire Initialized Event
     $mapper->fireEvent('initialized', $mapper);
     return $mapper;
 }
Exemplo n.º 7
0
 /**
  * Get a combo type.primaryKey
  *
  * @param  Mappable $entity
  * @return string
  */
 protected function getEntityHash(Mappable $entity)
 {
     $class = get_class($entity);
     $keyName = $this->relatedMapper->getManager()->mapper($class)->getEntityMap()->getKeyName();
     $hash = $class . '.' . $entity->getEntityAttribute($keyName);
     return $hash;
 }
Exemplo n.º 8
0
 /**
  * Make custom mapper custom commands available in repository
  *
  * @param  string $method
  * @param  array $parameters
  * @return mixed
  */
 public function __call($method, $parameters)
 {
     if ($this->mapper->hasCustomCommand($method)) {
         call_user_func_array(array($this->mapper, $method), $parameters);
     } else {
         throw new Exception("No method {$method} on " . get_class($this));
     }
 }
Exemplo n.º 9
0
 /**
  * Deduce the relationships that will be lazy loaded from the eagerLoads array
  *
  * @return array
  */
 protected function prepareLazyLoading()
 {
     $entityMap = $this->entityMap;
     if (!$entityMap->relationsParsed()) {
         $initializer = new MapInitializer($entityMap);
         $initializer->splitRelationsTypes($this->mapper->newInstance());
     }
     $singleRelations = $entityMap->getSingleRelationships();
     $manyRelations = $entityMap->getManyRelationships();
     $allRelations = array_merge($manyRelations, $singleRelations);
     return array_diff($allRelations, $this->eagerLoads);
 }
Exemplo n.º 10
0
 /**
  * By hooking to the mapper initialization event, we can extend it
  * with the softDelete capacity.
  * 
  * @param  \Analogue\ORM\System\Mapper 	$mapper 
  * @return void
  */
 protected function registerSoftDelete(Mapper $mapper)
 {
     $entityMap = $mapper->getEntityMap();
     // Add Scopes
     $mapper->addGlobalScope(new SoftDeletingScope());
     $host = $this;
     // Register 'deleting' events
     $mapper->registerEvent('deleting', function ($entity) use($entityMap, $host) {
         $deletedAtField = $entityMap->getQualifiedDeletedAtColumn();
         if (!is_null($entity->getEntityAttribute($deletedAtField))) {
             return true;
         } else {
             $time = new Carbon();
             $entity->{$deletedAtField} = $time;
             // Launch an update instead
             $host->manager->mapper(get_class($entity))->store($entity);
             return false;
         }
     });
     // Register RestoreCommand
     $mapper->addCustomCommand('Analogue\\ORM\\Plugins\\SoftDeletes\\Restore');
 }
Exemplo n.º 11
0
 /**
  * Get the attributes that have been modified since
  * the entity have been fetched from the database
  * 
  * @return array
  */
 public function getDirtyAttributes()
 {
     $attributes = $this->flattenEmbeddables($this->entity->getEntityAttributes());
     $id = $attributes[$this->keyName];
     $cachedAttributes = $this->mapper->getEntityCache()->get($id);
     $dirty = [];
     foreach ($attributes as $key => $value) {
         if ($this->isRelation($key) || $key == 'pivot') {
             continue;
         }
         if (!array_key_exists($key, $cachedAttributes) && !$value instanceof Pivot) {
             $dirty[$key] = $value;
         } elseif ($value !== $cachedAttributes[$key] && !$this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) {
             $dirty[$key] = $value;
         }
     }
     return $dirty;
 }
Exemplo n.º 12
0
 /**
  * Get a new instance for the entity
  * 
  * @param  array  $attributes 
  * @return Entity
  */
 public function getEntityInstance(array $attributes = array())
 {
     return $this->mapper->newInstance($attributes);
 }