Beispiel #1
0
 /**
  * To build a repository, either provide :
  *
  * - Mappable object's class name as a string
  * - Mappable object instance
  * - Instance of mapper
  *
  * @param Mapper|Mappable|string $mapper
  * @param EntityMap $entityMap (optionnal)
  *
  * @throws \InvalidArgumentException
  */
 public function __construct($mapper, EntityMap $entityMap = null)
 {
     if ($mapper instanceof Mappable || is_string($mapper)) {
         $this->mapper = Manager::getMapper($mapper, $entityMap);
     } else {
         if ($mapper instanceof Mapper) {
             $this->mapper = $mapper;
         } else {
             new InvalidArgumentException('Repository class constuctor need a valid Mapper or Mappable object.');
         }
     }
 }
 /**
  * Instantiate an user entity
  *
  * @return User
  */
 private function getEntity()
 {
     $entity = $this->getEntity();
     return $this->manager->mapper($entity)->newInstance();
 }
Beispiel #3
0
 /**
  * Define a polymorphic many-to-many relationship.
  *
  * @param  string $related
  * @param  string $name
  * @param  string $table
  * @param  string $foreignKey
  * @param  string $otherKey
  * @param  bool $inverse
  * @return \Analogue\ORM\Relationships\MorphToMany
  */
 public function morphToMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false)
 {
     $caller = $this->getBelongsToManyCaller();
     // First, we will need to determine the foreign key and "other key" for the
     // relationship. Once we have determined the keys we will make the query
     // instances, as well as the relationship instances we need for these.
     $foreignKey = $foreignKey ?: $name . '_id';
     $relatedMapper = $this->manager->mapper($related);
     $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getForeignKey();
     $table = $table ?: str_plural($name);
     return new MorphToMany($relatedMapper, $entity, $name, $table, $foreignKey, $otherKey, $caller, $inverse);
 }
Beispiel #4
0
 /**
  * Get the mapper instance for the entity
  * 
  * @param  Mappable $entity 
  * @return \Analogue\ORM\System\Mapper
  */
 protected function getMapper(Mappable $entity)
 {
     return Manager::getMapper($entity);
 }
 protected function getEntityKey(Mappable $entity)
 {
     $keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName();
     return $entity->getEntityAttribute($keyName);
 }
Beispiel #6
0
 protected function cachedArray(Mappable $entity)
 {
     // Flatten Value Objects as attributes
     $attributes = $this->flattenEmbeddables($entity->getEntityAttributes());
     $cache = [];
     foreach ($attributes as $key => $value) {
         if ($value instanceof ProxyInterface) {
             continue;
         }
         if ($value instanceof Mappable) {
             $class = get_class($value);
             $mapper = Manager::getMapper($class);
             $keyName = $mapper->getEntityMap()->getKeyName();
             $cache[$key] = new CachedRelationship($class . '.' . $value->{$keyName}, $this->getPivotValues($key, $value));
             continue;
         }
         if ($value instanceof EntityCollection) {
             $cache[$key] = [];
             foreach ($value as $relatedEntity) {
                 $hash = $this->getEntityHash($relatedEntity);
                 $cache[$key][$hash] = new CachedRelationship($hash, $this->getPivotValues($key, $relatedEntity));
             }
             continue;
         }
         $cache[$key] = $value;
     }
     return $cache;
 }
Beispiel #7
0
 /**
  * Create a mapper for a given entity (static alias)
  *
  * @param \Analogue\ORM\Mappable|string $entity
  * @param mixed $entityMap
  * @return Mapper
  */
 public static function getMapper($entity, $entityMap = null)
 {
     return static::$instance->mapper($entity, $entityMap);
 }
Beispiel #8
0
 /**
  * Execute a store command on a dirty entity
  * 
  * @param  mixed $entity 
  * @return void
  */
 protected function updateEntityIfDirty($entity)
 {
     $mapper = Manager::getMapper($entity);
     $checker = new StateChecker($entity, $mapper);
     $dirtyAttributes = $checker->getDirtyAttributes();
     if (count($dirtyAttributes) > 0) {
         $mapper->store($entity);
     }
 }
Beispiel #9
0
 /**
  * Get the mapper's instance for this entity class
  * 
  * @return \Analogue\ORM\System\Mapper
  */
 protected function getMapper()
 {
     return $this->manager->getMapper($this->class);
 }