FosRestBundle PATCH 操作阻止具有 null/default 值的更新实体
FosRestBundle PATCH action prevent update entity with null/default values
我已经在我的 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",
// ...
}
正如你在我的控制器中看到的那样,我已经设置了每个可更新的字段
$server->setAlias($updatedServer->getAlias())
->setMac($updatedServer->getMac())
->setSshUser($updatedServer->getSshUser())
// ...
因此,如果主体请求中的值是 null
,控制器会将其设置为 null
,它将对实体中设置的默认值执行相同的操作
我想为每个可更新字段设置一个 if
条件,但我将在其中设置 >20 条件...
我怎样才能防止它附加一个通用的和可重用的系统?
如果在执行请求之前没有设置这些值,是否可以忽略这些值?
也许在我的实体 class 中创建 callback
?
谢谢
编辑
我的 Server $server
是我要更新的当前服务器对象。它随请求而来。例如,当我发送此请求 /api/servers/2
时,我得到了 ID 为 2
.
的服务器正文
Server $updatedServer
是具有更新数据的正文。
我尝试了你的第二次编辑,但我得到了 500 error
"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",
// ...
}
像这样,不用写支票属性。
William Durand Don't PATCH like an idiot 对这种常见的错误用法有很好的参考。
编辑
我错了
您无需完全更新您的资源即可正确使用 PATCH。
您需要发送一组更改,如 link 中所述。
我能给你的更好的建议是使用 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.
这个很好的例子
我刚找到那个问题的答案
在您的实体中使用设置参数函数,这将更新您将在请求正文中提供的字段
App\Entity\Server
use Doctrine\Common\Inflector\Inflector;
public class Server {
public function setParameters($params) {
foreach ($params as $k => $p) {
if (!is_null($p)) { // here is the if statement
$key = Inflector::camelize($k);
if (property_exists($this, $key)) {
$this->{'set' . ucfirst($key)}($p);
}
}
}
return $this;
}
}
App\Controller\ServerApiController
public function patchAction(Server $server, Request $request, ConstraintViolationListInterface $validationErrors)
{
if ($validationErrors->count() > 0) {
return $this->handleBodyValidationErrorsView($validationErrors);
}
$data = json_decode($request->getContent());
$em->persist($server->setParameters($data););
$em->flush();
return $this->view([$server]);
}
它应该 return 服务器的内容 + 由您的请求主体更新的字段,这将在 $server->setParameter($data)
我已经在我的 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",
// ...
}
正如你在我的控制器中看到的那样,我已经设置了每个可更新的字段
$server->setAlias($updatedServer->getAlias())
->setMac($updatedServer->getMac())
->setSshUser($updatedServer->getSshUser())
// ...
因此,如果主体请求中的值是 null
,控制器会将其设置为 null
,它将对实体中设置的默认值执行相同的操作
我想为每个可更新字段设置一个 if
条件,但我将在其中设置 >20 条件...
我怎样才能防止它附加一个通用的和可重用的系统?
如果在执行请求之前没有设置这些值,是否可以忽略这些值?
也许在我的实体 class 中创建 callback
?
谢谢
编辑
我的 Server $server
是我要更新的当前服务器对象。它随请求而来。例如,当我发送此请求 /api/servers/2
时,我得到了 ID 为 2
.
Server $updatedServer
是具有更新数据的正文。
我尝试了你的第二次编辑,但我得到了 500 error
"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",
// ...
}
像这样,不用写支票属性。
William Durand Don't PATCH like an idiot 对这种常见的错误用法有很好的参考。
编辑
我错了
您无需完全更新您的资源即可正确使用 PATCH。 您需要发送一组更改,如 link 中所述。
我能给你的更好的建议是使用 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.
我刚找到那个问题的答案
在您的实体中使用设置参数函数,这将更新您将在请求正文中提供的字段
App\Entity\Server
use Doctrine\Common\Inflector\Inflector;
public class Server {
public function setParameters($params) {
foreach ($params as $k => $p) {
if (!is_null($p)) { // here is the if statement
$key = Inflector::camelize($k);
if (property_exists($this, $key)) {
$this->{'set' . ucfirst($key)}($p);
}
}
}
return $this;
}
}
App\Controller\ServerApiController
public function patchAction(Server $server, Request $request, ConstraintViolationListInterface $validationErrors)
{
if ($validationErrors->count() > 0) {
return $this->handleBodyValidationErrorsView($validationErrors);
}
$data = json_decode($request->getContent());
$em->persist($server->setParameters($data););
$em->flush();
return $this->view([$server]);
}
它应该 return 服务器的内容 + 由您的请求主体更新的字段,这将在 $server->setParameter($data)