Example #1
0
 public function testCommit()
 {
     //test insert
     $this->finder->create()->setName('Sylvester Stallone')->setPhone('+1-123-123');
     //test delete
     $this->finder->getByIdOrFalse(3)->delete();
     //test update
     $this->finder->getByIdOrFalse(2)->setName('Mr. Jack Smit');
     $this->orm->commit();
     //clean objects and see changes
     $this->assertEquals('Mike Smit', $this->finder->getByIdOrFalse(1)->getName());
     $this->assertEquals('Mr. Jack Smit', $this->finder->getByIdOrFalse(2)->getName());
     $this->assertEquals('Sylvester Stallone', $this->finder->getByIdOrFalse(4)->getName());
     $this->assertFalse($this->finder->getByIdOrFalse(3));
 }
Example #2
0
 public function setProperty($name, $value)
 {
     if ($name == 'id') {
         throw new \InvalidArgumentException('Cannot set ID of a model');
     }
     if (!isset($this->orm->config->models[$this->baseClass]->properties[$name])) {
         throw new \InvalidArgumentException("Cannot set unknown property: {$this->baseClass}.{$name}");
     }
     $propConfig = $this->orm->config->models[$this->baseClass]->properties[$name];
     if (!$propConfig->isSettable) {
         throw new \LogicException("Cannot set a readonly property: {$this->baseClass}.{$name}");
     }
     $type = $propConfig->type;
     try {
         $this->properties[$name] = $this->orm->typeConverter->convertOnSetter($type, $value, $propConfig->isNullable);
     } catch (ConversionImpossibleException $e) {
         throw new ConversionImpossibleException($e->getMessage() . " in {$this->baseClass} when setting {$name}", $e->getCode(), $e);
     }
     // при вызове например setRegionId мы должны помимо поля regionId ещё проставить
     // в модели поля, которые подтягиваются по связи через это поле (например regionName)
     foreach ($propConfig->dependentProxies as $propName => $proxy) {
         if ($value === null) {
             $this->properties[$proxy->localProperty] = null;
         } else {
             $foreignModel = $this->orm->getFinder($proxy->foreignModel)->getByIdOrFalse($value);
             if (!$foreignModel) {
                 throw new \Exception("Cannot set {$this->baseClass}.{$name}: there is no {$proxy->foreignModel} with ID {$value}");
             }
             $this->properties[$propName] = $foreignModel->getProperty($proxy->foreignProperty);
         }
     }
     $this->markAsChanged();
     return $this;
 }
Example #3
0
 public function __construct(ConnectionInterface $db, ClassNameProvider $classNameProvider, ModelObserver $modelObserver, TypeConverter $typeConverter, Config $config, CacheInterface $cache, EventDispatcher $eventDispatcher, Logger $logger, RoleHierarchyInterface $roleHierarchy, Validator $validator)
 {
     if (!$modelObserver instanceof DispatchedModelObserver) {
         throw new \LogicException('Model observer must be instance of DispatchedModelObserver for properly work of GracePlusSymfony extension');
     }
     parent::__construct($db, $classNameProvider, $modelObserver, $typeConverter, $config, $cache);
     $this->eventDispatcher = $eventDispatcher;
     $this->logger = $logger;
     $this->roleHierarchy = $roleHierarchy;
     $this->validator = $validator;
 }