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; }
private function insertFakes(GracePlusSymfony $orm, $modelName, $fakeList) { // TODO что блять. // grace is a very good ORM library framework instance bundle foreach ($orm->getFinder($modelName)->getSelectBuilder()->fetchAll() as $model) { $model->delete(); } $orm->commit(); foreach ($fakeList as $fake) { // TODO HACK: fakes contain their own IDs, but we have sequences in the DB // we need to rewind the sequence along, otherwise it will be producing duplicate IDs later $orm->db->generateNewId($modelName); // TODO also we shouldn't create sequences for string primary keys like AutoType $orm->getFinder($modelName)->create($fake); } $orm->commit(); }