我如何使用@ManyToMany Symfony4保留数据

问题描述 投票:0回答:1

[当我想要具有关系ManyToMany Department的导入数据(csv)主管时出现问题:主管(法语代理)已存储,但未创建该关系(表agent_departement)。代理类:

/**
 * @ORM\Entity(repositoryClass="App\Repository\AgentRepository")
 */
class Agent 
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * 
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Departement", inversedBy="agents")
     * @ORM\JoinTable(name="agent_departement")
     */
    private $departements;

public function __construct()
    {
        $this->departement = new ArrayCollection();
        $this->incidents = new ArrayCollection();
    }
  /**
     * @return Collection|departement[]
     */
    public function getDepartements(): Collection
    {
        return $this->departements;
    }

    public function addDepartement(departement $departement): self
    {
        if (!$this->departement->contains($departement)) {


            $departement->addAgent($this);
            $this->departement[] = $departement;
        }

        return $this;
    }

    public function removeDepartement(departement $departement): self
    {
        if ($this->departement->contains($departement)) {
            $this->departement->removeElement($departement);
        }

        return $this;
    }


班级部门:

/**
 * @ORM\Entity(repositoryClass="App\Repository\DepartementRepository")
 */
class Departement
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Region", inversedBy="departements")
     * @ORM\JoinColumn(nullable=false)
     */
    private $region;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Intervention", mappedBy="departements", orphanRemoval=true)
     */
    private $interventions;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Agent", mappedBy="departements")
     */
    private $agents;


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

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

    public function getDesignation(): ?string
    {
        return $this->designation;
    }

    public function setDesignation(string $designation): self
    {
        $this->designation = $designation;

        return $this;
    }

    public function getRegion(): ?Region
    {
        return $this->region;
    }

    public function setRegion(?Region $region): self
    {
        $this->region = $region;

        return $this;
    }

    /**
     * @return Collection|Intervention[]
     */
    public function getInterventions(): Collection
    {
        return $this->interventions;
    }

    public function addIntervention(Intervention $intervention): self
    {
        if (!$this->interventions->contains($intervention)) {
            $this->interventions[] = $intervention;
            $intervention->setDepartement($this);
        }

        return $this;
    }

    public function removeIntervention(Intervention $intervention): self
    {
        if ($this->interventions->contains($intervention)) {
            $this->interventions->removeElement($intervention);
            // set the owning side to null (unless already changed)
            if ($intervention->getDepartement() === $this) {
                $intervention->setDepartement(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|Agent[]
     */
    public function getAgents(): Collection
    {
        return $this->agents;
    }

    public function addAgent(Agent $agent): self
    {
        if (!$this->agents->contains($agent)) {
            $this->agents[] = $agent;
            $agent->addDepartement($this);
        }

        return $this;
    }

    public function removeAgent(Agent $agent): self
    {
        if ($this->agents->contains($agent)) {
            $this->agents->removeElement($agent);
            $agent->removeDepartement($this);
        }

        return $this;
    }

}

ImportController

 /**
     * @Route("/agent", name="import_agent")
     */
    public function importAgent(Request $request, DepartementRepository $departementRepository){
        $em = $this->getDoctrine()->getManager();
        $csv = Reader::createFromPath($request->files->get("myfile"), 'r');
        $csv->setDelimiter(';');
        $csv->setHeaderOffset(0); //set the CSV header offset
        $em = $this->getDoctrine()->getManager();
        foreach ($csv as $record) {
            $agent = new Agent();
            $agent->setIdrh($record["Idrh"]);
            $agent->setPoste($record["Poste"]);
            $agent->setPrenom(utf8_encode($record["Prenom"]));
            $agent->setNom(utf8_encode($record["Nom"]));
            $agent->setLibelleRegate($record["Libelle_regate"]);
            $agent->setGrade($record["Grade"]);
            $agent->setEmail($record["Email"]);
            $agent->setRit($record["rit"]);
            $agent->setTelephone($record["Telephone"]);
            $agent->setCodeRegate($record["Code_regate"]);



           $departements = explode(',',$record["Departement"]);
           foreach($departements as $dep){
                $depObject = $departementRepository->find($dep);
                $agent->addDepartement($depObject);

           }

           $em->persist($agent);
           $em->flush();
        }
        return $this->redirectToRoute('agent_index');
    }


转储重复部门idk为什么转储$ agent

 +"departement": ArrayCollection^ {#10496 ▼
    -elements: array:8 [▼
      0 => Departement^ {#10584 ▼
        -id: 78
        -designation: "Yvelines"
        -region: Region^ {#10673 ▶}
        -interventions: PersistentCollection^ {#11105 ▶}
        -agents: PersistentCollection^ {#11389 ▶}
      }
      1 => Departement^ {#10584 ▶}
      2 => Departement^ {#11423 ▶}
      3 => Departement^ {#11423 ▶}
      4 => Departement^ {#11436 ▶}
      5 => Departement^ {#11436 ▶}
      6 => Departement^ {#11449 ▶}
      7 => Departement^ {#11449 ▶}
    ]
  }
}

我有点绝望,我在灌木丛中挣扎,感谢您的回答,希望我表示清楚,并对我的英语不佳表示歉意

php symfony doctrine-orm doctrine many-to-many
1个回答
0
投票

在您的addDepartement函数中(并且在其他地方也类似地命名...)

$this->departement[] = $departement;

还有遏制检查,还有初始化

尽管您的字段名有很大不同。

private $departements; // <-- plural s
© www.soinside.com 2019 - 2024. All rights reserved.