Skip to content
Advertisement

symfony entities many to many, i add new fields

I try this code: Symfony: ManyToMany table extra columns

This new field is because the user have a diferent role for diferent events.

In the fist time i can add add the new fields on table user.evento, but now i cant. And i dont find the problem.

I get this fail:

An exception occurred while executing ‘INSERT INTO user_evento (user_id, evento_id) VALUES (?, ?)’ with params [3, 18]: SQLSTATE[HY000]: General error: 1364 Field ‘tipoinvitado’ doesn’t have a default value

why not detecting the other 2 fields ?

capture of my sql update with the new fields

capture

my entity UserHasEvento:

 
/**
* @ORMEntity
* @ORMTable(name="user_evento")
*/
class UserHasEvento
{
   /**
    * @ORMId
    * @ORMColumn(name="id", type="integer")
    * @ORMGeneratedValue(strategy="AUTO")
    */
   private $id;
 
    /**
     * @ORMManyToOne(targetEntity="Evento", cascade={"persist"}, fetch="LAZY")
     * @ORMJoinColumn(name="evento_id", referencedColumnName="id")
     */
    private $eventos;

    /**
     * @ORMManyToOne(targetEntity="User", cascade={"persist","remove"}, fetch="LAZY" )
     * @ORMJoinColumn(name="user_id", referencedColumnName="id",nullable=true)
     */
    private $users;

    /**
     * @ORMColumn(name="tipoinvitado", type="string", length=255, nullable=true)
     */
    private $tipoinvitado;

    /**
     * @var DateTime|null
     *
     * @ORMColumn(name="fechainscripcion", type="datetime", nullable=true)
     */
    private $fechainscripcion;    

    public function setTipoinvitado(string $tipoinvitado): self
    {
        $this->tipoinvitado = $tipoinvitado;
 
        return $this;
    }
 
    public function getTipoinvitado(): string
    {
        return $this->tipoinvitado;
    }

    public function getFechainscripcion()
    {
        return $this->fechainscripcion;
    }

    public function setFechainscripcion($fechainscripcion): self
    {
        $this->fechainscripcion = $fechainscripcion;

        return $this;
    }  
    public function __construct()
    {
        $this->createdAt= new DateTime('now');
    }
}

My controller update.

  public function asignarUsuario(Request $request, Evento $evento, Userhasevento $userhasevento){
        $user_repo = $this->getDoctrine()->getRepository(User::class);
        $users = $user_repo->findAll();    

        $evento = $this->getDoctrine()->getRepository(Evento::class)->findOneById($evento); 
        $form = $this->createForm(AsignarEventoFormType::class, $evento);
        $form->handleRequest($request);
        
        if($form->isSubmitted() && $form->isValid()){
            $users = $evento->GetUsers();
                foreach($users as $user){
               $user->addEvento($evento);       
                $evento->addUser($user);
                $userhasevento->SetTipoinvitado('normal');
                $userhasevento->setFechainscripcion(new DateTime('now'));
                $em = $this->getDoctrine()->getManager();
                $em->persist($evento);
                $em->persist($userhasevento);
                $em->flush();
            }
                return $this->redirect($this->generateUrl('evento_detalle', ['id' => $evento->getId()]));
 
        }        
        return $this->render('evento/AsignarEvento.html.twig',[
            'form' => $form->createView()
        ]);
    }

Advertisement

Answer

Is your field $tipoinvitado disappear? You might have changed your relation and the null boolean of $tipoinvitado at some point. You made your migration after changing any relation?

This can happen when you change your relations and had already data stored in you DB in a field that disappear because of you relation change.

I might be able to help you, don’t hesitate giving more info

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement