gpt4 book ai didi

php - FosRestBundle PATCH 操作阻止具有空值/默认值的更新实体

转载 作者:搜寻专家 更新时间:2023-10-31 21:25:51 24 4
gpt4 key购买 nike

我在我的 serverController 上制作了一个有效的 patchAction 来更新现有服务器中的一个或某些字段。

实际上我的 patchAction 看起来像这样

/*
* @ParamConverter("updatedServer", converter="fos_rest.request_body")
*
* @return View
*/
public function patchAction(Server $server, Server $updatedServer, ConstraintViolationListInterface $validationErrors)
{
if ($validationErrors->count() > 0) {
return $this->handleBodyValidationErrorsView($validationErrors);
}

$server->setAlias($updatedServer->getAlias())
->setMac($updatedServer->getMac())
->setSshUser($updatedServer->getSshUser())
->setSshPort($updatedServer->getSshPort())
->setIpmiAddress($updatedServer->getIpmiAddress())
->setIpmiLogin($updatedServer->getIpmiLogin())
->setIpmiPassword($updatedServer->getIpmiPassword())
->setMysqlHost($updatedServer->getMysqlHost())
->setMysqlRoot($updatedServer->getMysqlRoot())
->setWebServer($updatedServer->getWebServer())
->setWebServerSslListen($updatedServer->getWebServerSslListen())
->setWebServerSslPort($updatedServer->getWebServerSslPort())
->setMysqlServer($updatedServer->getMysqlServer())
->setSuphp($updatedServer->getSuphp())
->setFastcgi($updatedServer->getFastcgi())
->setNadminCompliant($updatedServer->getNadminCompliant())
->setEmailCompliant($updatedServer->getEmailCompliant())
->setAvailable($updatedServer->getAvailable())
->setEnvironment($updatedServer->getEnvironment())
->setInstalledAt($updatedServer->getInstalledAt());

if (null !== $updatedServer->getOs()) {
$os = $this->getDoctrine()->getRepository('AppBundle:Os')->findBy(['id' => $updatedServer->getOs()->getId()]);
$server->setOs($os[0]);
}

if (null !== $updatedServer->getPuppetClasses()) {
$puppetClass = $this->getDoctrine()->getRepository('AppBundle:PuppetClass')->findBy(['id' => $updatedServer->getPuppetClasses()[0]->getId()]);
$server->setPuppetClasses($puppetClass);
}

if (null !== $updatedServer->getPuppetTemplates()) {
$puppetTemplate = $this->getDoctrine()->getRepository('AppBundle:PuppetTemplate')->findBy(['id' => $updatedServer->getPuppetTemplates()[0]->getId()]);
$server->setPuppetTemplates($puppetTemplate);
}

if (null !== $updatedServer->getBackupModel()) {
$backupModel = $this->getDoctrine()->getRepository('AppBundle:BackupModel')->findBy(['id' => $updatedServer->getBackupModel()->getId()]);
$server->setBackupModel($backupModel[0]);
}

$em = $this->getDoctrine()->getManager();

$em->persist($server);
$em->flush();

return $this->view([$updatedServer, $server]);
}

问题出在尝试仅更新一个或几个字段时。我设置了一个 JSON 主体来更改数据。

{
"mac": "ff:ff:ff:ff:ff:ff"
}

发送请求后,JSON 正文将如下所示

// This is what $updatedServer get in my controller
{
"id": null,
"name": null,
"alias": null,
"notes": null,
"hosted_domain": null,
"mac": "ff:ff:ff:ff:ff:ff",
// ...
}

正如您在我的 Controller 中看到的,我已经设置了每个可更新的字段

$server->setAlias($updatedServer->getAlias())
->setMac($updatedServer->getMac())
->setSshUser($updatedServer->getSshUser())
// ...

因此,如果请求主体中的值为 null, Controller 会将其设置为 null,它会对实体中设置的默认值做同样的事情

我的想法是为每个可更新字段创建一个 if 条件,但我将在其中设置 >20 条件...

我怎样才能防止它附加一个通用且可重用的系统?

如果在执行请求之前没有设置这些值,是否可以忽略这些值?

也许在我的实体类中创建回调

谢谢

编辑

我的 Server $server 是我要更新的当前服务器对象。它随请求而来。例如,当我发送此请求 /api/servers/2 时,我得到了 ID 为 2 的服务器主体。

Server $updatedServer 是更新数据的主体。

我尝试了您的第二次编辑,但出现了 500 错误

"Unable to guess how to get a Doctrine instance from the request information."

因为我无法同时获取我要修补的服务器和正文(使用ParamConverter)。

最佳答案

您可以使用 PATCH 方法来防止这种情况,如 the specification解释一下(正确的方式)。

顾名思义,PATCH 方法用于发送更新现有资源的补丁。

要正确使用它,您需要发送资源的全新状态。
换句话说,您必须发送资源的所有属性,包括那些未更改的属性。

因此,如果您发送每个属性及其相应的值,您的补丁将被正确应用。

例子:

{
"id": 1, # Identifier, never change
"name": [oldValue],
"alias": [oldValue],
"notes": [oldValue],
"hosted_domain": [oldValue],
"mac": "ff:ff:ff:ff:ff:ff",
// ...
}

像这样,不需要为每个属性写一张支票。

Don't PATCH like an idiot 是对这种常见错误用法的一个很好的引用。来自 William Durand。

编辑

我错了

您无需完全更新您的资源即可正确使用 PATCH。您需要发送一组更改,如给定链接中所述。

我能给你的更好建议是使用 ParamConverter它通过标识符检索您的对象,并根据您提供的字段为您进行更新。

EDIT2

我的意思是:

/*
* @ParamConverter("server", converter="fos_rest.request_body")
*
* @return View
*/
public function patchAction(Server $server, ConstraintViolationListInterface $validationErrors)
{
if ($validationErrors->count() > 0) {
return $this->handleBodyValidationErrorsView($validationErrors);
}

$em = $this->getDoctrine()->getManager();

$em->persist($server);
$em->flush();

// ...
}

否则,您需要一个 Assembler 对象作为合并的中间步骤。
看看这个很好的例子 handling PATCH requests through FOSRest .

关于php - FosRestBundle PATCH 操作阻止具有空值/默认值的更新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36335703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com