dongpenggan6812 2015-03-18 21:07
浏览 45
已采纳

Doctrine2验证是否修改了属性

I am building a bundle for uploading many photos.

I use a table to display my photo's forms.

visual of web page

My problem is, when I click on "Modifier" nothing is done if I didn't change "Activé" or "Titre".

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
        )
    );
}

If I had this line after isValid(), that's work but when "Activé" or "Titre" are changed, my photos are uploaded twice.

$photo->upload();

How I can force an upload if my attribute which are persisted in my entity are not changed?

I have taken a look in doctrine annotation but, I didn't find solution.

Ajax submit

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);
    }
});
  • 写回答

1条回答 默认 最新

  • drdr123456 2015-03-19 08:37
    关注

    You can check if your $request have files inside. This will do the trick.

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

    Edit by olive007:

    Thank you help I have found my solution:

    $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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测