How can you avoid inserting a null record in the d

2019-04-02 07:48发布

I'm working with Symfony2 to create a management system of a school. Needed to register students record their data and their representatives (who could be the parents). In the following link you can see the code I used for it.

look here (Already solved the problem indicated in the link)

But I have a problem that I can not solve. That the second charge should be mandatory, because a student can have only one responsible. With this code if I leave blank data from the second responsible, it is created a new responsibility with all null data in the database and the new student is referred to it. What I need it is that the new student save the reference of the first responsible and the second is zero, and that the second responsible is not created.

Now, I explain with an example the structure of the database 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 the reference of those 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 in the student entity:

    /**
 * 
 * @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 modified the __toString() function in Parents entity:

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

After all this, it still keeps the second nulls responsible if I leave it blank.

The controller used in this case is on the link. I show below already corrected:

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 do not have a solution to the problem, I have added a test condition. In this case when the second responsible is null, which makes the condition is assigning the identifier of the first responsible to the second one, so that both are the same. Being the Parents form embedded in the Students form, I do not know how I can stop if the second responsible is null not be persisted to then not saved to the database. I do not know if the problem is something to this or not.

I'm starting on this, so I need your help kindly, thank you.

0条回答
登录 后发表回答