通过ID的Symfony fullcalendar删除事件

时间:2019-05-25 02:05:13

标签: jquery ajax symfony controller fullcalendar

我试图根据他的ID删除事件,我得到了ID,但控制器给出了错误,因为它期望有一个对象并给出了一个字符串。


事件点击会填充模态数据。

eventClick: function(info) {
    var eventObj = info.event;      
    //filling data          
        $('#ev_title').html(eventObj.title);    
        $('#ev_start').html('<b>Fecha inicio: </b> ' + eventObj.start);
        $('#ev_end').html('<b>Fecha fin: </b> ' + eventObj.end);
        $('#ev_desc').html('<b>Descripcion: </b>' + 
        eventObj.extendedProps.description);
        $('#ev_mail').html('<b>Correo destino: </b> ' + 
        eventObj.extendedProps.mail);
        $('#ev_id').html(eventObj.id);},

按钮重定向到删除功能。

<div id="myModal" class="modal fade" style="z-index: 9999;">
<div class="modal-dialog">
    <div class="modal-content">
         <div class="modal-header">         

            <h4 id="ev_title" class="modal-title"></h4> 
        --> <h4 id="ev_id" class="modal-title"></h4> 

        </div>
        <!-- dialog body -->
        <div class="modal-body">                

        <p id="ev_start" class="modal-body"></p>
        <p id="ev_end" class="modal-body"></p>
        <p id="ev_mail" class="modal-body"></p>
        <p id="ev_desc" class="modal-body"></p>            

        </div>
        <!-- dialog buttons -->
        <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn btn-primary">Cerrar</button>
        <button type="button" class="btn btn-danger" onclick="borrar()">Borrar evento</button>      
        </div>          
    </div>
</div>


function borrar(){              
            var data = $('#ev_id').html();              

    $.ajax({                
        type: "POST",           
        data: { id: data },
        url: "{{ path('borrar') }}", 

    function(response){ 
    if(response.code === 200 && response.success){ alert('success!'); } 
    else{ alert('something broken');}},

        success: function(data) 
        {
            console.log(data);
            alert('OK');
        } 
    });         
}

XHR POST ID


控制器:

public function deleteAction(Request $request){ 

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

$data = $request->request->get('id');

$em->remove($data);
$em->flush();

这是错误消息:

  

EntityManager#remove()期望参数1为实体对象,给出字符串

2 个答案:

答案 0 :(得分:2)

实际上,有很多方法可以实现这一点,但是最好和最简单的做法是将id嵌入到url中,如下所示:

/**
 * @Route("/delete/{id}")
 */

然后使用Symfony的ParamConverter获取如下对象:

/**
 * @Route("/delete/{id}")
 */
public function deleteAction(YourClass $yourClassObject, EntityManagerInterface $em)
{
    $em->remove($object);
    $em->flush();
}

答案 1 :(得分:0)

您应该从ID中检索对象并删除该对象。 $object = $em->getDoctrine()->getRepository(Entity::class)->find($request->get("id"),然后像执行操作一样删除$ object: $em->remove($object); $em->flush();

提示:如果您将Symfony> = 3.4用于默认服务配置,则可以在函数的参数中插入EntityManagerInterface $em, ClassRepository $repo

public function deleteAction(Request $request, EntityManagerInterface $em, ClassRepository $repo){ 
$object = $repo->find($request->get('id');
$em->remove($object);
$em->flush();