提交具有实体数据的表单后,我想检查某些属性是否已从其数据库状态更改。如果是这样,我需要执行与其没有相同的行动。我写了下面的代码。似乎问题是getRepository()->findOneById()
没有从数据库中获取值,而是从某种缓存中获取值。也许Symfony已经考虑在此时更改Dropoffpoint
,因此Dropoffpoint->getCommission()
的值始终与从getRepository->findOneById()->getCommission()
检索到的值相同。
public function editDOPAction(Request $request, DOP $DOP) {
$form = $this->createForm(DOPType::class, $DOP);
$deleteForm = $this->createDeleteForm($DOP);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$existingDOP = $em->getRepository('AppBundle:Dropoffpoint')->findOneById($DOP->getId()); # get DOP with same id
if ($existingDOP) { # we found one
if ($existingDOP->getCommission() != $DOP->getCommission()) {
# commission changed (destructive!)
$em->remove($existingDOP); #soft-delete the old DOP
$em->persist($DOP); # persist a new DOP
} else {
$em->persist($DOP); # no destructive changes to DOP, just update as usual
}
$em->flush();
}
}