How can you avoid inserting a null record into the database using a one-to-many association using doctrine?

I work with Symfony2 to create a school management system. It is necessary to register students writing down their data and their representatives (who may be parents). In the following link, you can see the code that I used for it.

look here (the problem indicated in the link has already been resolved)

But I have a problem that I cannot solve. The second charge should be mandatory, as the student can have only one person in charge. With this code, if I leave empty data from the second responsible, a new responsibility is created with all the zero data in the database, and a new student refers to it. What I need is that the new student will keep the link of the first responsible, and the second is zero, and the second responsible is not created.

Now I will explain an example of the database structure that I have and what I get:

Student table in the database

ID NAME SURNAMES ..... RESPONSIBLE_1 RESPONSIBLE_2 1 .... 2 .... 3 name_student surnames ... 5 6 

Responsible table in the database

 ID NAME SURNAMES ..... 1 .... 2 .... 3 .... 4 .... 5 name surnames date .... 6 NULL NULL NULL NULL NULL NULL .... 

As you can see in the example, the student contains a link to those who are responsible, but the second charge is zero. Instead, I get the following:

Student table in the database

 ID NAME SURNAMES ..... RESPONSIBLE_1 RESPONSIBLE_2 1 .... 2 .... 3 name_student surnames ... 5 NULL 

Responsible table in the database

 ID NAME SURNAMES ..... 1 .... 2 .... 3 .... 4 .... 5 name surnames date .... 

I tried adding nullable=false to the student object:

  /** * * @ORM\ManyToOne(targetEntity="Parents", inversedBy="students", cascade={"persist"}) * @ORM\JoinColumn(name="responsible2_id", referencedColumnName="id", nullable=false) * @Assert\Valid */ private $responsible2; 

And I get this error:

 Catchable Fatal Error: Method School\BackendBundle\Entity\Parents::__toString() must return a string value in /opt/lampp/htdocs/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/ORMInvalidArgumentException.php line 199 

I changed the __toString () function in the Parents object:

 public function __toString() { if(is_null($this->getName())) { return 'NULL'; } return $this->getName(); } 

In the end, he still holds the second zero in charge if I leave it empty.

The controller used in this case is located by reference. Below I will show the correction:

StudentController.php

 /** * Creates a new Student entity. * */ public function createAction(Request $request) { $entity = new Student(); $form = $this->createCreateForm($entity); $form->handleRequest($request); if ($form->isValid()) { $responsible1 = $em->getRepository('BackendBundle:Parents')->findResponsible($entity->getResponsible1()->getNid()); $responsible2 = $em->getRepository('BackendBundle:Parents')->findResponsible($entity->getResponsible2()->getNid()); if($responsible1){ $entity->setResponsible1($responsible1); } if($responsible2){ $entity->setResponsible2($responsible2); } //This test conditions if($entity->getResponsible2()->getNid() == "" ) { $entity->setResponsible2($entity->getResponsible1()); } //Select values for other fields. $entity->getResponsible1()->setUsername($entity->getResponsible1()->getNid()); $entity->getResponsible2()->setUsername($entity->getResponsible2()->getNid()); $entity->getResponsible1()->setPassword($entity->getResponsible1()->getNid()); $entity->getResponsible2()->setPassword($entity->getResponsible2()->getNid()); $em = $this->getDoctrine()->getManager(); $em->persist($entity); $em->flush(); return $this->redirect($this->generateUrl('student_show', array('id' => $entity->getId()))); } return $this->render('BackendBundle:Student:new.html.twig', array( 'entity' => $entity, 'form' => $form->createView(), )); } 

While I have no solution to the problem, I have added a verification condition. In this case, when the second responsible is zero, which forces the condition to assign the identifier of the first responsible to the second, so that they are both the same. Being the Parents form built into the Students form , I don’t know how I can stop if the second responsible null is not saved and then not stored in the database. I do not know what the problem is or not.

I start with this, so I need your help kindly, thanks.

+2
database php symfony doctrine2
source share

No one has answered this question yet.

See similar questions:

3
Symfony2: Prevent Duplication in a Database with Many to One Forms

or similar:

41
Doctrine - a new entity has been found through relationships
6
Symfony2: PrePersist / PreUpdate event does not fire
4
How to avoid duplicate entries in a many-to-many relationship with the Doctrine?
2
Symfony2: How to avoid duplication of objects that already exist in the database?
one
Set entityrine entity boolean field to 0 instead of null
0
Class "AppBundle \ Entity \ Organization" is not a valid entity or mapped superclass
0
The doctrine of the merger does not work as expected
0
Doctrine ORM: populate the join column of related objects aggressively
0
Triple Relationship Card
-one
Error gettins save operation

All Articles