dppn67180
2015-03-08 18:14
浏览 51
已采纳

Symfony2如何使用doctrine更新生成Ajax请求

I am developing a interface for edit page of my website. CKeditor is used. I want use Ajax method to save the modification. My text is stock in DB. I use Doctrine to access to him.

script jQuery

$(".loading").hide();

$('[name="contentsForm"]').submit(function() {
    $('[name="contentsForm"]').hide();
    $("#contents .loading").show();
    $.ajax({
        type: "POST",
        url: "{{ path('AdminAjaxEditText', {'page' : 'index', 'description' : 'contents'})}}",
        data: "{'data': '" + $('#contentsForm_data').val() + "'}",
        cache: false,
        success: function(data){
            $('[name="contentsForm_data"]').html(data);
            CKupdate();
            $('[name="contentsForm"]').show();
            $("#contents .loading").hide();
        }
    });
    return false;
});

Controller php

public function ajaxEditAction($page, $description, Request $request) {

    $em = $this->getDoctrine()->getManager();

    $text = $em->getRepository("FDMWebsiteBundle:Text")->find(array("page" => $page, "description" => $description));
    $form = $this->get('form.factory')->createNamed($description."Form", new TextType(), $text);

    if ($form->handleRequest($request)->isValid()) {
        $em->flush();
    }

    return $this->render("Bundle:Admin:textForm.html.twig", array(
        "form" => $form->createView()
        ));
}

Template twig

{{ form_start(form, {'action': ''}, {'method': 'POST'}) }}
{{ form_errors(form) }}
{{ form_widget(form.data, { 'attr': {'class': 'ckeditor'} }) }}
<input type="submit" class="saveTextBtn btn btn-primary pull-right" value="Enregistrer"/>
<div class="clearfix"></div>
{{ form_rest(form) }}
{{ form_end(form) }}

My problem is when is click on save the Ajax run but nothing is saved in my DB ?

What wrong I have done ?

I don't understand why my code is not flush but is displayed in textarea after the Ajax request.

Sorry for my English, I am learning it...

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douwei9973 2015-03-10 18:35
    最佳回答

    I have found my problems

    First I need to set the charset to utf-8 and get data form CKEditor not form the textarea

    scriptCharset: "utf-8",
    data: "data=" + CKEDITOR.instances['contentsForm_data'].getData()
    

    http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#getData

    Second : My ajax controller need to use getter's request. The handleRequest method of form isn't working, I don't knonw why. If someone can give me a explain I am looking for it.

    if($request->isXmlHttpRequest()) {
        $data = $request->request->get('data');
        $text->setData($data);
        $em->flush();
    }
    

    Tutorial and some post use this solution but I don't understand why handlerResquest fail ?

    Third: I need to configure CKEDITOR to don't use entities

    CKEDITOR.config.entities = false;
    

    http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-entities

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题