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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示