/** * 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); }
/** * * @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(); }
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(); }
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()); }
/** * 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()); }
/** * 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; }
/** * 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; }
/** * 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)); } }
/** * 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); }
/** * 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'); }
/** * 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; }
/** * Get a new instance for the entity * * @param array $attributes * @return Entity */ public function getEntityInstance(array $attributes = array()) { return $this->mapper->newInstance($attributes); }