2015-03-18 22 views
1

我正在构建用于上传许多照片的包。Doctrine2验证属性是否持久被修改

我用桌子来显示我的照片的形式。

visual of web page

我的问题是,当我点击“修改”什么都不做如果我没有改变“激活”或“滴定度”。

public function ajaxEditPhotoAction(Request $request, $id) { 
    $error = null; 
    $em = $this->getDoctrine()->getManager(); 

    $repository = $em->getRepository("FDMFileUploaderBundle:Photo"); 
    $photo = $repository->find($id); 
    $form = $this->get('form.factory')->create(new PhotoType(), $photo); 

    if ($request->isMethod('POST')) { 
     $form->bind($request); 
     if ($form->isValid()) { 
      $em->flush(); 
     } 
     else { 
      $error = "Mauvaise donnée"; 
     } 
    } 
    else { 
     $error = "Wrong Method"; 
    } 

    return $this->render('FDMFileUploaderBundle:Default:editPhoto.html.twig', array(
     "error" => $error 
     ) 
    ); 
} 

如果我isValid()后出现此行,这是工作,但何时“主动”或“标题”改变,我的照片上传两次。

$photo->upload(); 

我怎么能强制实施上载如果我的属性,它在我的坚持实体没有改变?

我看了doctrine注解,但我没有找到解决方案。

阿贾克斯提交

var data = new FormData(this); 
$.ajax({ 
    type: "POST", 
    url: url, 
    data: data, 
    contentType: false, 
    cache: false, 
    processData:false, 
    success: function(data) { 
     alert("Success"); 
    }, 
    error: function(xhr, textStatus, errorThrown) { 
     alert("Error "+xhr+textStatus+errorThrown); 
    } 
}); 

回答

0

您可以检查您的$request有内部文件。这将做到这一点。

if ($form->isValid() || $request->files->count() > 0) { 
     $photo->upload(); 
     $em->flush(); 
    } 

编辑由olive007:

,谢谢你帮我找到了我的解决方案:

$nbFile = $request->files->count(); 

if ($form->isValid()) { 
    $uow = $em->getUnitOfWork(); 
    $uow->computeChangeSets(); 
    if ($nbFile == 1 && !$uow->isScheduledForUpdate($photo)) { 
     // title and enabled aren't modified but file is 
     $photo->upload(); 
    } 
    else { 
     $em->flush(); 
    } 
}