In OneToMany relation, Image is null in Symfony 6

46 views Asked by At

In OneToMany relation, Image is null in Symfony 6. I have an Entity Project which has a relation OneToMany with an Entity Teams. On persisting into db I'm getting the following error:

An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null

Code:

        $project->setDonorsPdf($donorPdfName);
        $this->em->persist($project);
        foreach($form->getData()->getTeams() as $key => $coorindator) {
            $teams = new Teams();
            $coorindatorImage = $teamImageUpload->uploadTeamImage($form->get('teams')[$key]['cropped_file']->getData(), $coorindator);
            $teams->setImage($coorindatorImage);
            $this->em->persist($teams);
        }
        $this->em->flush();    
        $this->addFlash('message', 'Project added successfully.');

if I do dd($project) then the image is null, but if I do dd($teams) above $this->em->persist($teams); then I can see the image.

#[ORM\OneToMany(mappedBy: 'project', targetEntity: Teams::class, cascade : ['persist'])]
private Collection $teams;

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

Project Form:

        ->add('youtube_video_id', TextType::class, [
            'required' => false,
            'label' => 'YouTube Embed Video Id',
            'empty_data' => null
        ])
        ->add('teams', CollectionType::class, [
            'entry_type' => CoordinatorType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'label' => false,
            'by_reference' => false,
        ])

Teams Form:

        ->add('image', FileType::class, [
            'mapped' => false,
        ])

As far as I know, it's because the image mapped=> false. Any solution where the image holds the value even if the attribute mapped is false?

Edit:

            $teams->setImage($coorindatorImage);
            $project->addTeam($teams);

So I did this, but when I do dd($project) I see 2 $teams one has image and the other doesn't.

0

There are 0 answers