/** * @param bool $lazyChecks * @return Model | null * @throws GraphException */ public static function getByRequest($lazyChecks = false) { $req = Graphene::getInstance()->getRequest(); $requestModels = ModelFactory::createByRequest($req, null, $lazyChecks); if (isset($requestModels[self::stcName()])) { return $requestModels[self::stcName()]; } else { throw new GraphException('Sent model is not valid for' . self::stcName(), 400, 400); } }
/** * Sovrascrive un model fornendo id e versione * * @param Model $model * @return Model Il model modificato * @throws GraphException * @internal param $ <b>Model</b> model da modificare [id e versione obbligatori]* <b>Model</b> model da modificare [id e versione obbligatori] */ public function update(Model $model) { Log::debug('calling storage driver for update'); $model->setLazy(false); if ($model->getId() == null) { throw new GraphException('Unavailable ' . $model->getModelName() . ' id', ExceptionsCodes::BEAN_STORAGE_ID_UNAVAILABLE, 400); } if ($model->getVersion() == null) { throw new GraphException('Unavailable ' . $model->getModelName() . ' version', ExceptionsCodes::BEAN_STORAGE_VERSION_UNAVAILABLE, 400); } if (!$model->isValid()) { throw new GraphException('Error on storage, ' . $model->getModelName() . ' is corrupt: ' . $model->getLastTestErrors(), ExceptionsCodes::BEAN_STORAGE_CORRUPTED_BEAN, 400); } $bkpContent = $model->getContent(); $model->setLazy(true); $model->setContent(array('id' => $bkpContent['id'])); $readed = $this->read($model); if ($readed === null) { throw new GraphException($model->getModelName() . ' not found', ExceptionsCodes::BEAN_STORAGE_ID_NOT_FOUND); } if ($readed->getVersion() != $bkpContent['version']) { throw new GraphException($model->getModelName() . ' version mismatch, reload your ' . $model->getModelName() . ' instance for updates', ExceptionsCodes::BEAN_STORAGE_VERSION_MISMATCH, 400); } $model->setContent($bkpContent); $model->setVersion($model->getVersion() + 1); $updated = $this->driver->update($this->serializeForDb($model)); $model = ModelFactory::createByDbSerialization($updated); if ($model === null) { throw new GraphException('Updated model is corrupt', ExceptionsCodes::BEAN_STORAGE_CORRUPTED_BEAN, 400); } else { if (is_array($model)) { return $model[0]; } else { return $model; } } }