使用学说和 Symfony 4 将 ManyToOne Table 插入数据库时​​出现问题

Problem while Inserting to ManyToOne Table into DB with doctrine and Symfony 4

我正在使用 Symfony 4 和 doctrine,我尝试将数据插入 ManyToOne 实体关系 是 PromotionAffecte with Entity Promotion (oneToMany) and Utilisateur(oneToMany) 但是我最终得到一个错误,我无法解析
推广实体:

/**
 * @ORM\Entity(repositoryClass=PromotionRepository::class)
 */
class Promotion
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=10, nullable=true)
     * @Assert\Choice({"Cadeau", "Remise"})
     */
    private $Type;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Assert\NotBlank(message="Veuillez ajouter description")
     */
    private $description;

    /**
     * @ORM\Column(type="integer")
     * @Assert\NotBlank(message="Veuillez ajouter score")
     */
    private $scoreMin;

    /**
     * @ORM\OneToMany(targetEntity=PromotionAffecte::class, mappedBy="idPromo", orphanRemoval=true)
     */
    private $utilisateur;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     */
    private $title;

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

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getType(): ?string
    {
        return $this->Type;
    }

    public function setType(?string $Type): self
    {
        $this->Type = $Type;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getScoreMin(): ?int
    {
        return $this->scoreMin;
    }

    public function setScoreMin(int $scoreMin): self
    {
        $this->scoreMin = $scoreMin;

        return $this;
    }

    public function __toString()
    {
        return (string) $this->getId();
    }

    /**
     * @return Collection|PromotionAffecte[]
     */
    public function getUtilisateur(): Collection
    {
        return $this->utilisateur;
    }

    public function addUtilisateur(PromotionAffecte $user): self
    {
        if (!$this->utilisateur->contains($user)) {
            $this->utilisateur[] = $user;
            $user->setIdPromo($this);
        }

        return $this;
    }

    public function removeUtilisateur(PromotionAffecte $user): self
    {
        if ($this->utilisateur->removeElement($user)) {
            // set the owning side to null (unless already changed)
            if ($user->getIdPromo() === $this) {
                $user->setIdPromo(null);
            }
        }

        return $this;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(?string $title): self
    {
        $this->title = $title;

        return $this;
    }
}

实用实体:

/**
 * @ORM\Entity(repositoryClass=UtilisateurRepository::class)
 */
class Utilisateur
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $nom;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $prenom;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $addresse;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $numTel;

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

    /**
     * @ORM\Column(type="string", length=10)
     */
    private $Type;

    /**
     * @ORM\OneToMany(targetEntity=PromotionAffecte::class, mappedBy="idUser", orphanRemoval=true)
     */
    private $promotion;

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

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }

    public function getPrenom(): ?string
    {
        return $this->prenom;
    }

    public function setPrenom(string $prenom): self
    {
        $this->prenom = $prenom;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getAddresse(): ?string
    {
        return $this->addresse;
    }

    public function setAddresse(?string $addresse): self
    {
        $this->addresse = $addresse;

        return $this;
    }

    public function getNumTel(): ?int
    {
        return $this->numTel;
    }

    public function setNumTel(?int $numTel): self
    {
        $this->numTel = $numTel;

        return $this;
    }

    public function getScore(): ?int
    {
        return $this->score;
    }

    public function setScore(int $score): self
    {
        $this->score = $score;

        return $this;
    }

    public function getType(): ?string
    {
        return $this->Type;
    }

    public function setType(string $Type): self
    {
        $this->Type = $Type;

        return $this;
    }

    /**
     * @return Collection|PromotionAffecte[]
     */
    public function getPromotion(): Collection
    {
        return $this->promotion;
    }

    public function addPromotion(PromotionAffecte $promotion): self
    {
        if (!$this->promotion->contains($promotion)) {
            $this->promotion[] = $promotion;
            $promotion->setIdUser($this);
        }

        return $this;
    }

    public function removePromotion(PromotionAffecte $promotion): self
    {
        if ($this->promotion->removeElement($promotion)) {
            // set the owning side to null (unless already changed)
            if ($promotion->getIdUser() === $this) {
                $promotion->setIdUser(null);
            }
        }

        return $this;
    }

    public function __toString()
    {
        return (string) $this->getId();
    }
}

促销影响实体:

/**
 * @ORM\Entity(repositoryClass=PromotionAffecteRepository::class)
 */
class PromotionAffecte
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="date")
     */
    private $delai;

    /**
     * @ORM\ManyToOne(targetEntity=Utilisateur::class, inversedBy="promotion")
     * @ORM\JoinColumn(nullable=false)
     */
    private $idUser;

    /**
     * @ORM\ManyToOne(targetEntity=Promotion::class, inversedBy="Utilisateur")
     * @ORM\JoinColumn(nullable=false)
     */
    private $idPromo;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getDelai(): ?\DateTimeInterface
    {
        return $this->delai;
    }

    public function setDelai(\DateTimeInterface $delai): self
    {
        $this->delai = $delai;

        return $this;
    }

    public function getIdUser(): ?Utilisateur
    {
        return $this->idUser;
    }

    public function setIdUser(?Utilisateur $idUser): self
    {
        $this->idUser = $idUser;

        return $this;
    }

    public function getIdPromo(): ?Promotion
    {
        return $this->idPromo;
    }

    public function setIdPromo(?Promotion $idPromo): self
    {
        $this->idPromo = $idPromo;

        return $this;
    }

}

控制器代码:

/**
     * @Route ("/affectePromotion", name="affecterPromotion")
     */
    public function affectPromotion(Request $request)
    {
        $affectation = new PromotionAffecte();
        $form = $this->createForm(PromotionAffecteType::class);
        $form->add("submit", SubmitType::class );
        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()) {
            //$affectation = $form->getData();

            $repo = $this->getDoctrine()->getRepository(Promotion::class)->find($form["idUser"]->getData());
            $repo1 = $this->getDoctrine()->getRepository(Utilisateur::class)->find($form["idPromo"]->getData());

            $affectation->setIdUser($repo);
            $affectation->setIdPromo($repo1);
            $affectation->setDelai($form["delai"]->getData());

            $em = $this->getDoctrine()->getManager();
            $em->persist($affectation);
            $em->flush();

            return $this->redirectToRoute('promotionList');
        }
    return $this->render('promotion_affecte/affectePromotion.html.twig', [

            'form' => $form->createView()
        ]);
    }

这是PromotionAffecte表格:

class PromotionAffecteType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('delai', DateType::class)
            ->add('idUser', IntegerType::class)
            ->add('idPromo', IntegerType::class)
            ->add('delai', DateType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => PromotionAffecte::class,
        ]);
    }
}

这是我最终遇到的错误:
在 属性 路径“idUser”处给出的“App\Entity\Utilisateur 或 null”类型的预期参数,“int”。

您没有正确的PromotionAffecteTypeidUseridPromo 是对象,使用 EntityType 如下所示:

class PromotionAffecteType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('delai', DateType::class)
            ->add('idUser', EntityType::class,[
                'class' => Utilisateur::class,
                'choice_label' => 'nom',
            ])
            ->add('idPromo', EntityType::class,[
                'class' => Promotion::class,
                'choice_label' => 'title',
            ])
            ->add('delai', DateType::class)
            ->add('submit', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => PromotionAffecte::class,
        ]);
    }
}

并将控制器中的方法更改为:

   /**
     * @Route ("/affectePromotion", name="affecterPromotion")
     */
    public function affectPromotion(Request $request)
    {
        $form = $this->createForm(PromotionAffecteType::class);
    
        $form->handleRequest($request);
    
        if($form->isSubmitted() && $form->isValid()) {
            $affectation = $form->getData();
            $em = $this->getDoctrine()->getManager();
            $em->persist($affectation);
            $em->flush();
    
            return $this->redirectToRoute('promotionList');
        }
        return $this->render('promotion_affecte/affectePromotion.html.twig', [
            'form' => $form->createView()
        ]);
    }