Ejemplo n.º 1
0
 public function testFinder()
 {
     $finder = $this->orm->getFinder('TaxiPassenger');
     $this->assertTrue($finder instanceof TaxiPassengerFinder);
     $finder2 = $this->orm->getFinder('TaxiPassenger');
     $this->assertEquals(spl_object_hash($finder), spl_object_hash($finder2));
 }
Ejemplo n.º 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;
 }