Php 更新实体(如果已存在)
[设置]Php 更新实体(如果已存在),php,symfony,Php,Symfony,[设置] Symfony 3.4 从另一个环境发布数据 [问题] 我目前正在直接从外部POST数据持久化/创建一个新实体。 解析后,我刷新所有数据以创建新条目 如果我的实体已经存在于数据库中,我应该在我的控制器中做什么来更新它呢 配送箱实体 发布数据 uuid=9a16dc07-902c-6115-6e9b-acb800968e25&url=https://sim9885.agni.lindenlab.com:12043/cap/4d6fe9c7-5fb7-77e8-8dbc-565dac4
- Symfony 3.4
- 从另一个环境发布数据
解析后,我刷新所有数据以创建新条目 如果我的实体已经存在于数据库中,我应该在我的控制器中做什么来更新它呢 配送箱实体 发布数据
uuid=9a16dc07-902c-6115-6e9b-acb800968e25&url=https://sim9885.agni.lindenlab.com:12043/cap/4d6fe9c7-5fb7-77e8-8dbc-565dac4b0e49
控制器动作
如果
uuid
是唯一的,则它可能如下所示
控制器动作
祝您一切顺利谢谢。有趣的是,我现在才从你的回答中知道editAction从来没有
$entity=newentity
。对你来说,这是一个小小的修正,它是findOneBy(数组('uuid'=>$content['uuid'])`findonebyuid
是一个神奇的方法,它返回与'findOneBy(array('uuid'=>$content['uuid'))相同的结果代码>有。啊~我不知道。谢谢谢谢你的回复,但我这里没有使用任何表格。不需要它们。
class DeliveryBox {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="uuid", type="string", length=40, unique=true)
*/
private $uuid;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255, unique=true)
*/
private $url;
}
/**
* Creates a new deliveryBox entity.
*
* @Route("/registerbox/", name="register_box")
* @Method({"POST"})
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function newAction(Request $request) {
$deliveryBox=new Deliverybox();
//Parse POST data and set DeliveryBox values
$content=array();
parse_str($request->getContent(), $content);
$deliveryBox->setUuid($content['uuid']);
$deliveryBox->setUrl($content['url']);
$em=$this->getDoctrine()->getManager();
$em->persist($deliveryBox);
$em->flush();
return $this->redirectToRoute('deliverybox_show', array('id'=>$deliveryBox->getId(),));
}
/**
* Creates a new deliveryBox entity.
*
* @Route("/registerbox/", name="register_box")
* @Method({"POST"})
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function newAction(Request $request) {
// Parse POST data
$content = array();
parse_str($request->getContent(), $content);
// Get entity manager instance
$em = $this->getDoctrine()->getManager();
// Find existing delivery box by UUID
$deliveryBox = $em->getRepository(DeliveryBox::class)->findOneByUuid($content['uuid']);
if (null === $deliveryBox) {
// If delivery box with current UUID is not found, create one
$deliveryBox = new DeliveryBox();
$deliveryBox->setUuid($content['uuid']);
}
// Update URL
$deliveryBox->setUrl($content['url']);
$em->persist($deliveryBox);
$em->flush();
return $this->redirectToRoute('deliverybox_show', array('id'=>$deliveryBox->getId(),));
}
/**
* This is sample code, which receives id of entity
* to edit and create the edit form and do the update,
* if form is submitted and valid. See this for more info
* https://symfony.com/doc/3.4/controller.html
* Edit an existing entity.
*
* @Route("/registerbox-edit/{id}", name="register_box_edit")
* @Method({"POST"})
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function editAction(Request $request, Deliverybox $deliverybox)
{
$editForm = $this->createForm('AppBundle\Form\DeliveryboxType', $deliverybox);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('deliverybox_show', array('id'=>$deliveryBox->getId(),));
}
}