我需要向表Worker添加一些数据。主要思想是每个工人都有自己的识别通行证。每个工人都由自己设定。它们都是字符串。
表格结构:
NAME
SURNAME
PHONENUMBER
IDENTPASS
我的问题是:
我需要:
但每次我编辑这个表单的内容,但是在表单中更改不同的行(例如NAME)我会收到错误,因为他说IDENTPASS的值存在。因为他想再次保存它并且已经存在这个价值......
现在我得到了许多IF,这是我的错误IDEA。并非一切都有效。
MY CONTROLLER:
public function AcmeAction() {
if (!is_null($id)){
$employee=$this->getDoctrine()->getRepository('MainCoreBundle:Employee')->find($id);
$passFromBase = $employee->getIdentpass();
$employeeForm = $this->createForm(new EmployeeType(), $employee);
if ($request->getMethod() == "POST") {
$employeeForm->bind($request);
if ($employeeForm->isValid()) {
//1. IF value of ROW is thesame for CURRENT USER ->SAVE()
$pracownik =$employeeRepo->find($id);
if(( $pracownik->getIdentpass()==$employeeForm->get('identpass')->getData())){
$employee->setIdentpass($employeeForm->get('identpass')->getData());
$this->getDoctrine()->getManager()->persist($employee);
$this->getDoctrine()->getManager()->flush();
return $this->redirect($this->generateUrl('employee_index'));
}
//IF VALUE OF ROW FROM FORM EXIST IN DATA BASE
// IF YES (so bad) redirect back to form
elseif($employeeRepo->findOneByIdentpass($employeeForm->get('identpass')->getData())==true){
return $this->redirect($this->generateUrl('employee_edit', array('id' => $id)));
}
// IF NO(so value is new) THEN SAVE TO BASE
else{
$employee->setIdentpass($employeeForm->get('identpass')->getData());
$this->getDoctrine()->getManager()->persist($employee);
$this->getDoctrine()->getManager()->flush();
return $this->render('MainAdminBundle:Employee:entity.html.twig', $this->getViewConstants(array('form' => $employeeForm->createView())));
}
}
}
}
//IF NEW EMPLOYEE
else{
$employeeForm = $this->createForm(new EmployeeType(), $employee);
if ($request->getMethod() == "POST") {
$employeeForm->bind($request);
if ($employeeForm->isValid()) {
//IF IDENTPASS EXIST IN BASE redirect back to form
if($employeeRepo->findOneByIdentpass($employeeForm->get('identpass')->getData())==true){
return $this->redirect($this->generateUrl('employee_edit', array('id' => $id)));
}
else{//IF VALUE of Identpass DONT EXIST IN BASE SAVE()
$employee->setIdentpass($employeeForm->get('identpass')->getData());
$employee->setSpot($spot);
$this->getDoctrine()->getManager()->persist($employee);
$this->getDoctrine()->getManager()->flush();
return $this->redirect($this->generateUrl('employee_index'));
}
}
}
}
return $this->render('MainBundle:Employee:entity.html.twig', $this->getViewConstants(array('form' => $employeeForm->createView(), 'pass'=>$passFromBase)));
}
答案 0 :(得分:0)
好的,我不知道是否覆盖了所有内容,但我认为这可以开始。请注意我们使用相同的表格进行插入nad更新。这绝不是完全完成的代码,你可以插件和播放,而是抓住你可以做的表面。
public function insertEditAction($id){
$employeeRepo = $this->getDoctrine()->getRepository('MainCoreBundle:Employee');
if (!is_null($id)){
$employee=$employeeRepo->find($id); // For update
}else{
$employee = new Employee(); // For insert
}
$employeeForm = $this->createForm(new EmployeeType(), $employee);
if ( $request->getMethod() === "POST"){
$form->bind($request);
if ( $form->isValid()){
$em = $this->getDoctrine()->getManager();
$existingByIdentPass = $employeeRepo->findOneByIdentpass($employee->getIdentpass());
// If there is some other employee with this IDENT_PASS then redirect back to form
if($existingByIdentPass !== NULL && $existingByIdentPass->getId() !== $employee->getId()){
return $this->redirect($this->generateUrl('employee_edit', array('id' => $id)));
}
// New employee, set the SPOT and PERSIST
// calling persist is only required when creating new record
if ( $employee->getId() === NULL ){
$employee->setSpot($spot);
$em->persist($employee);
}
// Flush the changes
$em->flush();
return $this->redirect($this->generateUrl('employee_index'));
}
}
return $this->render('MainBundle:Employee:entity.html.twig', $this->getViewConstants(array('form' => $employeeForm->createView(), 'pass'=>$passFromBase)));
}