示例#1
0
 public function __construct(Entity\Reflection $reflection)
 {
     if (!$reflection->hasAdapter()) {
         throw new QueryException("Can not create query because entity " . $reflection->getClassName() . " has no adapter defined!");
     }
     $this->entityReflection = $reflection;
 }
示例#2
0
 /**
  * Get option
  *
  * @param string $key
  *
  * @return mixed
  *
  * @throws Exception\InvalidArgumentException
  */
 public function getOption($key)
 {
     if (!$this->hasOption($key)) {
         throw new Exception\InvalidArgumentException("Option " . $key . " not defined on " . $this->entityReflection->getClassName() . "::\$" . $this->name . "!");
     }
     return $this->options[$key];
 }
示例#3
0
 public function saveChanges($primaryValue, Connection $connection, Entity $entity)
 {
     $reflection = Entity\Reflection::load($entity);
     if (!$reflection->hasPrimary()) {
         throw new Exception\InvalidArgumentException("Only entity with primary can save changes!");
     }
     $sourceAdapter = $connection->getAdapter($this->sourceReflection->getAdapterName());
     $primaryName = $reflection->getPrimaryProperty()->getName();
     switch ($entity->getChangeType()) {
         case Entity::CHANGE_ATTACH:
             $adapterQuery = $sourceAdapter->createUpdateOne($this->getSourceResource(), $this->getPrimaryKey(), $primaryValue, [$this->getReferencingKey() => $entity->{$primaryName}]);
             $sourceAdapter->execute($adapterQuery);
             break;
         default:
             break;
     }
 }
示例#4
0
 /**
  * Find records by set of primary values
  *
  * @param array $primaryValues
  * @param array $associate
  *
  * @return Entity\Collection
  *
  * @throws Exception\RepositoryException
  */
 public function findPrimaries(array $primaryValues, array $associate = [])
 {
     $entityReflection = Entity\Reflection::load($this->getEntityName());
     if (!$entityReflection->hasPrimary()) {
         throw new Exception\RepositoryException("Method can not be used because entity " . $this->getEntityName() . " has no primary property defined!");
     }
     if (empty($primaryValues)) {
         throw new Exception\RepositoryException("Values can not be empty!");
     }
     try {
         return $this->query()->select()->setFilter([$entityReflection->getPrimaryProperty()->getName() => [Filter::EQUAL => $primaryValues]])->associate($associate)->run($this->connection);
     } catch (Exception\QueryException $e) {
         throw new Exception\RepositoryException($e->getMessage());
     }
 }
示例#5
0
 /**
  * Gets data which should be serialized to JSON
  *
  * @return array
  */
 public function jsonSerialize()
 {
     $output = [];
     foreach (Entity\Reflection::load(get_called_class())->getProperties() as $propertyName => $property) {
         $value = $this->{$propertyName};
         if ($value instanceof Entity\Collection || $value instanceof Entity) {
             $output[$propertyName] = $value->jsonSerialize();
         } elseif ($value instanceof \DateTime && $property->getType() === Entity\Reflection\Property::TYPE_DATE) {
             $output[$propertyName] = (array) $value;
             $output[$propertyName]["date"] = $value->format(self::$dateFormat);
         } else {
             $output[$propertyName] = $value;
         }
     }
     return array_merge($output, $this->_getPublicPropertyValues());
 }
示例#6
0
 /**
  * Convert entity to simple array
  *
  *  @param Entity $entity
  *
  *  @return array
  */
 public function unmapEntity(Entity $entity)
 {
     $output = [];
     foreach ($entity->getData() as $propertyName => $value) {
         $property = Entity\Reflection::load($entity)->getProperty($propertyName);
         // Skip associations & readonly
         if ($property->hasOption(Entity\Reflection\Property::OPTION_ASSOC) || !$property->isWritable()) {
             continue;
         }
         $output[$property->getName(true)] = $this->unmapValue($property, $value);
     }
     return $output;
 }
示例#7
0
 protected function getEntityReflection()
 {
     return Reflection::load($this->getRepository()->getEntityName());
 }
示例#8
0
 /**
  * Get entity by primary value
  *
  * @param mixed $value
  *
  * @return Entity|false
  */
 public function getByPrimary($value)
 {
     foreach ($this->data as $entity) {
         $primaryPropertyName = Entity\Reflection::load($entity)->getPrimaryProperty()->getName();
         $primaryValue = $entity->{$primaryPropertyName};
         if ($primaryValue === $value && $primaryValue !== null) {
             return $entity;
         }
     }
     return false;
 }