Hello, I have that a user can have one or several certificates and a certificate only belongs to one user.
this is the users 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,
)));
}
When saving the idcertificate it 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.
Let's go by parts there are several things:
->add('certificado', EntityType::class, array( 'class' => 'AppBundle:Certificado', 'choice_label' => 'nombre_o_el_campo_que_quieras', ));
Despite everything, you should show us something more about how your classes are built in order to help you. I hope I've helped.