Symfony 4/5 ERROR 1048 The user_id field cannot be empty! It is not empty!

Solved
malo91 Posted messages 55 Status Member -  
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   -
hello hello! here I am again with a question for my ongoing project, just 15 days new to symfony! since yesterday, I have an error that, despite its precision in the message, has not allowed me to find the answer! it was working before: I'm currently CREATING a trick and my id is not empty, so I don't understand! could you please help me?
according to my research, I also looked at my relationships: to no avail :(
here is my controllerTrick: (for the addition)

 public function new(Request $request): Response { $trick = new Trick(); $form = $this->createForm(TrickType::class, $trick); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $pictures = $form->get('picture')->getData(); foreach ($pictures as $picture) { $fichier = md5(uniqid()) . '.' . $picture->guessExtension(); $picture->move( $this->getParameter('pictures_directory'), $fichier ); $img = new Picture(); $img->setName($fichier); $trick->addPicture($img); } $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($trick); $entityManager->flush(); return $this->redirectToRoute('trick_index'); } return $this->render('trick/new.html.twig', [ 'trick' => $trick, 'form' => $form->createView(), ]); } 


my TrickType file:
class TrickType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name') ->add('description') ->add('createdAt') ->add('updateAt') ->add('category') ->add('picture', FileType::class, [ 'label'=> false, 'multiple'=> true, 'mapped'=> false, 'required'=> false ]) ; }


and finally my entityTrick since I'm in creation (which was working :( )

class Trick { /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $name; /** * @ORM\Column(type="string", length=355, nullable=true) */ private $description; /** * @ORM\Column(type="datetimetz") */ private $createdAt; /** * @ORM\Column(type="datetime") */ private $updateAt; /** * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="tricks") * @ORM\JoinColumn(nullable=false) */ private $category; /** * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="trick") */ private $comments; /** * @ORM\Column(type="string", length=255, nullable=true) */ private $picture; /** * @ORM\Column(type="string", length=100, nullable=true) */ private $videos; /** * @ORM\OneToMany(targetEntity=Picture::class, mappedBy="trick", * orphanRemoval=true, cascade={"persist"}) */ private $pictures; /** * @ORM\OneToMany(targetEntity=Video::class, mappedBy="trick") */ private $video; /** * @ORM\ManyToOne(targetEntity=User::class, inversedBy="trick") * @ORM\JoinColumn(nullable=false) */ private $user; 


thank you so much if you see why such a silly error has been bothering me for a day now, thank you for your help

4 answers

jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Hello,

According to my research, I also looked into my connections: to no avail :(


Without looking too closely... it seems that you are expecting a user

 /** * @ORM\ManyToOne(targetEntity=User::class, inversedBy="trick") * @ORM\JoinColumn(nullable=false) */ 

(this value cannot be NULL)
I rarely use Symfony... but... haven't you forgotten to "pass" the user in your creation?

.
Best regards,
Jordane
0
malo91
 
uh no since I'm logged in! :)
0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Humm....
Are you sure that something like this wouldn't solve your issue?

 $trick->setUser($this->getUser()); 
0
malo91 Posted messages 55 Status Member 1
 
However, I just made a dump and unfortunately I have:
Normalized Format App\Entity\Trick {#508 ▼ -id: null -name: "bonjour" -description: "oipipipi" -createdAt: DateTime @1451606400 {#1010 ▶} -updateAt: DateTime @1451606400 {#1270 ▶} -category: App\Entity\Category {#1306 ▶} -comments: Doctrine\ORM\PersistentCollection {#1534 …} -picture: null -videos: Doctrine\Common\Collections\ArrayCollection {#506 …} -pictures: Doctrine\ORM\PersistentCollection {#1533 …} -video: Doctrine\ORM\PersistentCollection {#1544 …} -user: null }


so my id is indeed null :( I don't understand.
0
malo91 Posted messages 55 Status Member 1
 
Wow, what happiness!! Thank you Jordan45, you are the best :)
yessssss it works :) thank you but since I like to understand, I would really like to know why it was necessary to add this when it worked before without it!!

thank you very much
thank you for your opinions
0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Since we don't know what you changed between when it worked and now, it's impossible to tell you.
You may have changed the property of the user_id column by setting it to disallowed null.

In any case, remember to mark the topic as resolved.
0