Problem in symfony security relationship

1

Hello I have a user can have one or more certificates and a certificate belongs only to a user

this is the user entity

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Table(name="app_users")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 */
class User implements UserInterface, \Serializable
{

     /**
     * @ORM\Column(type="integer")
     */
    private $idcertificado;


    /**
     * @ORM\ManyToOne(targetEntity="Certificado", inversedBy="users")
     * @ORM\JoinColumn(name="idcertificado", referencedColumnName="id")
     */
    private $certificado;

....

And the certified entity

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * certificado
 *
 * @ORM\Table(name="certificado")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CertificadoRepository")
 */
class certificado
{

    /**
     * @ORM\OneToMany(targetEntity="User", mappedBy="certificado")
     */
    private $users;

    public function __construct()
    {
        $this->users = new ArrayCollection();
    }

....

The Controller to create a new user is

public function newAction(Request $request)
{
    $user = new User();
    $form = $this->createForm('AppBundle\Form\UserType', $user);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $encoder = $this->container->get('security.password_encoder');
        $encoded = $encoder->encodePassword($user, $user->getPassword());
        $user->setPassword($encoded);
        $user->setUsername($user->getEmail());
        $user->setRol('admin'');//Este solo es el rol de usuario
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush($user);

        return $this->redirectToRoute('user_show', array('id' => $user->getId()));
    }

    return $this->render('user/new.html.twig', array(
        'user' => $user,
        'form' => $form->createView(),
    ));
}

The form is

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class UserType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('username')
        ->add('password',PasswordType::class)
        ->add('email',EmailType::class)
        ->add('isActive')
        ->add('idcertificado',ChoiceType::class, array(
    'choices'  => array(
        'Seleccione el rol' => null,
        'Basico' => 1,
        'Avanzado' => 2,
                       )));
    }

At the time of saving the ID certificate comes null. I do not understand why. This only happens to me with this. With the relationship of a normal entity this does not happen to me.

    
asked by semidf des 03.02.2017 в 03:14
source

1 answer

1

Let's go in parts there are several things:

  • In the user entity you should not save the id certificate as such, when you save the Certificate object, you can obtain it by instantiating the user and with something similar to $ user - > getCertificate () -> getId () . Moreover, in the table of the database the id of the certificate will be saved in both fields if you do it as you have written, it is redundant.
  • I do not know if it will affect but call the certified class - > Certificate.
  • The last and most important thing is that the User form is not well constructed. Outside of knowing if you have implemented the setIdcertificado function (which would be as I have already told you a very bad practice) you should use EntityClass in the following way:

->add('certificado', EntityType::class, array( 'class' => 'AppBundle:Certificado', 'choice_label' => 'nombre_o_el_campo_que_quieras', ));

You should expose us in spite of everything else as your classes are built to be able to help you. I hope I have helped you.

    
answered by 03.02.2017 / 09:57
source