我有以下问题:
我想在将对象保存到数据库之前检查一些东西:
这是我的控制器:
/**
* Edits an existing Document entity.
*
* @Route("/{id}", name="document_update")
* @Method("PUT")
* @Template("ControlBundle:Document:edit.html.twig")
*/
public function updateAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ControlBundle:Document')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Document entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new DocumentType(), $entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$document = $em->getRepository('ControlBundle:Document')->findOneBy(array(
'id' => $id,
));
if ($document->getCount() > 100)
$em->flush();
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
在我的数据库中,我有:
id count .......
23 110
在我的表格中我编辑:
id count .......
23 34
但是当我这样做时:
$document = $em->getRepository('ControlBundle:Document')->findOneBy(array(
'id' => $id,
));
//here $document->getCount() return 34; ------WHY? should return 110!!!
if ($document->getCount() > 100)
$em->flush();
最好的问候:D