[设置]
[问题]
我目前正在直接从外部POST数据持久化/创建新实体 解析后,我刷新所有数据以创建我的新条目。
如果我的实体已存在于DB中,我应该如何在我的控制器中更改我的实体。
DeliveryBox实体
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;
}
POST数据
uuid=9a16dc07-902c-6115-6e9b-acb800968e25&url=https://sim9885.agni.lindenlab.com:12043/cap/4d6fe9c7-5fb7-77e8-8dbc-565dac4b0e49
控制器操作
/**
* 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(),));
}
答案 0 :(得分:1)
如果uuid
是唯一的,它可能看起来像这样。
控制器操作
/**
* 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(),));
}
答案 1 :(得分:0)
/**
* 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(),));
}
}
一切顺利