Php Symfony2表单:在Ajax上创建实体成功
我正在使用Symfony2,我正在尝试在不重新加载页面的情况下创建新的实体集合。我的控制器工作正常,但我对Ajax有问题,我对它不太熟悉。下面是我已经拥有的代码。当我按下提交按钮时,新的实体保存在数据库中,但实体并没有出现在页面上 采集控制器Php Symfony2表单:在Ajax上创建实体成功,php,jquery,ajax,symfony,Php,Jquery,Ajax,Symfony,我正在使用Symfony2,我正在尝试在不重新加载页面的情况下创建新的实体集合。我的控制器工作正常,但我对Ajax有问题,我对它不太熟悉。下面是我已经拥有的代码。当我按下提交按钮时,新的实体保存在数据库中,但实体并没有出现在页面上 采集控制器 /** * @Route("/create-collection", name="collection_create_collection") * @Template() */ public function createAction(Request
/**
* @Route("/create-collection", name="collection_create_collection")
* @Template()
*/
public function createAction(Request $request)
{
$collection = new Collection();
$form = $this->createForm(
new CollectionType(),
$collection,
array('method' => 'POST',
'action' => $this->generateUrl('collection_create_submit'),
)
);
return array('collection'=>$collection, 'form' => $form->createView());
}
/**
* @Route("/collection_create_submit", name="collection_create_submit")
*/
public function createSubmitAction(Request $request)
{
$collection = new Collection();
$user = $this->getUser();
$form = $this->createForm(
new CollectionType(),
$collection,
array('method' => 'POST',
)
);
$form->handleRequest($request);
if ($form->isValid() && $form->isSubmitted()) {
$colname = $form["name"]->getData();
$existing = $this->getDoctrine()->getRepository('CollectionBundle:Collection')->findBy(['name' => $colname, 'user' => $user]);
if ($existing) {
return new JsonResponse(['error' => 'Collection with such name already exists']);
}
$em = $this->getDoctrine()->getManager();
$em->persist($collection);
$em->flush();
return new JsonResponse(array(
'success' => $collection
));
}
}
create.html.twig
{% include 'CollectionBundle:Collection:collectionJS.html.twig' %}
<div class="collection-create">
<h3 id="create-collection">Create a collection</h3>
<a class="close-reveal-modal" aria-label="Close">×</a>
{{ form_start(form, {'attr' : {'action' : 'collection_create_collection', 'id': 'create-collection-form'}}) }}
{{ form_widget(form) }}
<a class="button custom-close-reveal-modal" aria-label="Close">Cancel</a>
<button type="submit" value="create" class="right" onclick="createCollection();">Create</button>
{{ form_end(form) }}
</div>
<script type="application/javascript">
$('a.custom-close-reveal-modal').on('click', function(){
$('#externalModal').foundation('reveal', 'close');
});
</script>
function createCollection(){
var $form = $('#create-collection-form');
$($form).submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: $form.attr('action'),
data: $form.serialize()
}).done(function( data ) {
if(data.error)
{
console.log(data.error);
} else if(data.success) {
var collection = data.success;
$('.griditems').prepend('<p>' + collection + '</p>');
$('#externalModal').foundation('reveal', 'close');
}
});
});
}
如何在Ajax中获得此响应?JsonResponse无法将实体转换为JSON。您需要使用像JMSSerializerBundle这样的序列化程序库,或者在实体内实现序列化方法 检查此处提供的答案。
您必须返回一个简单的$collection对象,因为标准条令实体对象太大。我的建议是:
return new JsonResponse(array(
'success' => $collection->toArray()
));
以及向实体类添加新方法:
public function toArray(){
return array(
'id' =>$this->getId(),
'name'=>$this->getName(),
// ... and whatever more properties you need
); }
在这里看一看:第一眼看上去像createCollection();(js函数)是一个绑定(未定义)变量表单的提交。。。该按钮是否实际触发绑定和提交?你检查了你的javascript中是否触发了提交吗?@nixoschu提交没有触发,但我修复了它。现在它会触发,但我不接收实体实体实体是否至少保存在DB中但未返回?下一件事就是@Atan对你的回答,看看这个
public function toArray(){
return array(
'id' =>$this->getId(),
'name'=>$this->getName(),
// ... and whatever more properties you need
); }