Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用@ManyToMany Symfony4持久化数据_Php_Symfony_Doctrine Orm_Doctrine_Many To Many - Fatal编程技术网

Php 如何使用@ManyToMany Symfony4持久化数据

Php 如何使用@ManyToMany Symfony4持久化数据,php,symfony,doctrine-orm,doctrine,many-to-many,Php,Symfony,Doctrine Orm,Doctrine,Many To Many,当我需要导入数据(csv)官员与多个部门的关系时,我遇到了问题:官员(法语中的代理)已存储,但未创建关系(表代理\部门)。 代理类别: /** * @ORM\Entity(repositoryClass="App\Repository\AgentRepository") */ class Agent { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer")

当我需要导入数据(csv)官员与多个部门的关系时,我遇到了问题:官员(法语中的代理)已存储,但未创建关系(表代理\部门)。 代理类别:

/**
 * @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;
    }

}

进口控制器

 /**
     * @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 ▶}
    ]
  }
}

我有点走投无路了,谢谢你的回答,我希望在你的
adddepartment
函数中(在其他地方也有类似的名字…),我的英语很差,我很清楚,很抱歉

还有安全壳检查,还有初始化

不过,您的财产名称完全不同

private $departements; // <-- plural s

private$departments;//department
在构造函数中初始化,php将很乐意使用它,而doctrine不会注意到您的
adddepartment
函数(在其他地方也有类似的名称)中此非托管属性的任何差异

还有安全壳检查,还有初始化

不过,您的财产名称完全不同

private $departements; // <-- plural s
private$departments;//department
是在构造函数中初始化的,php很乐意使用它,而doctrine没有注意到这个非托管属性的任何差异