Symfony2:在一个表单中更新/创建两个操作
我尝试将两个操作,更新和创建,放在一个表单中:Symfony2:在一个表单中更新/创建两个操作,symfony,twig,Symfony,Twig,我尝试将两个操作,更新和创建,放在一个表单中: <form id="form-add1" method="post" action="{{ entity.id ? path('entities_edit') : path('entities_new')}}"> <div class="choice-fields"> <div class="move-left"> <label for="title">
<form id="form-add1" method="post" action="{{ entity.id ? path('entities_edit') : path('entities_new')}}">
<div class="choice-fields">
<div class="move-left">
<label for="title">title</label>
<input class="form-control" type="text" id="title" name="title" value="{{ entity.title }}" >
</div>
<div class="move-left">
<div class="color-style">
<div class="input-name">Color</div>
<input type="color" name="color" class="color-picker" id="color" value="{{ entity.color }}" >
</div>
</div>
</div>
<div class="clear"></div>
<div>
<label for="description">Description</label>
<textarea id="description" name="description">{{ entity.description }}</textarea>
</div>
<div class="btn-add">
<button type="submit" class="save">Enregistrer</button>
<button type="reset" class="reset">Réintialiser</button>
</div>
<div id="here"></div>
<div class="clear"></div>
</form>
更新
:
public function newAction(Request $request)
{
$entity = new Entities();
$form = $this->createForm(new EntitiesType(), $entity);
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('AppBundle:Entities')->findAll();
$em = $this->getDoctrine()->getManager();
$entity->setColor($request->get('color'));
$entity->setTitle($request->get('title'));
$entity->setDescription($request->get('description'));
$em->persist($entity);
$em->flush();
$identity = $entity->getId();
if($request->isXmlHttpRequest())
{
$json = json_encode(array(
'entities' => $request->get('title'),
'identity' => $identity
));
$response = new \Symfony\Component\HttpFoundation\Response();
$response->headers->set('Content-Type', 'application/json');
$response->setContent($json);
return $response;
}
return $this->redirectToRoute('entities_index');
}
public function editAction(Request $request, Entities $entity, $id)
{
$entity = $em->getRepository('AppBundle:Entities')->find($id);
$entity->setTitle($request->get('title'));
$entity->setColor($request->get('color'));
$entity->setDescription($request->get('description'));
$em->persist($entity);
$em->flush();
$json = json_encode(array());
return $this->redirectToRoute('Entities_index');
}
我的路线:
entities_new:
path: /new
defaults: { _controller: "AppBundle:Entities:new" }
methods: [GET, POST]
entities_edit:
path: /{id}/edit
defaults: { _controller: "AppBundle:Entities:edit" }
methods: [GET, POST]
当一个实体被调用以更新时,一个新的实体被添加。有人能解释一下吗?更新方法的签名是错误的。它应该是:
public function editAction(Request $request, Entities $entity)
还是这个
public function editAction(Request $request, $id)
在第一种情况下,您使用ParamConverter
功能通过Request
中的id自动加载实体,在这种情况下,您没有使用find($id)
方法,因为实体已经加载
在第二种情况下,通过手动find
方法从数据库加载实体
在签名中,$id被定义为第三个参数,因此它将始终为空,因此它将始终加载新实体。但您也修改了现有实体,因此基本上为其分配了一个空ID
这就是它被插入的原因。在您的编辑方法中删除第二个参数,这样您就有了请求和id。然后将您的细枝“entities\u edit”路径更新为
path('entities\u edit',{'id':entityId})
您需要将模板变量“entityId”添加到表单页面中。谢谢您的回答,对不起,我真的是一个初学者,那么我如何才能修复我的实体。请仔细阅读我的答案。解释就在这里。如果在此之后,您仍然不知道如何更改代码,请阅读此页:。如果,在那之后,您仍然不知道,那么您可能应该配对编码一段时间。