我如何使用@ManyToMany Symfony4 保存数据
How i persist data with @ManyToMany Symfony4
当我想要导入具有 ManyToMany 部门关系的数据 (csv) 官员时遇到问题:官员(法语代理人)在商店但未创建关系(table agent_departement)。
特工 class :
/**
* @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;
}
Class部门:
/**
* @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 ▶}
]
}
}
拐弯抹角,我有点绝望,谢谢你的回答,我希望我说清楚了,抱歉我的英语不好
在您的 addDepartement
函数中(在其他地方也有类似的命名...)
$this->departement[] = $departement;
还有包含检查,还有初始化
不过你的 属性 名字很不一样。
private $departements; // <-- plural s
因为 $this->departement
是在构造函数中初始化的,php 会愉快地使用它,而 doctrine 没有注意到这个非托管的任何差异 属性
当我想要导入具有 ManyToMany 部门关系的数据 (csv) 官员时遇到问题:官员(法语代理人)在商店但未创建关系(table agent_departement)。 特工 class :
/**
* @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;
}
Class部门:
/**
* @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 ▶}
]
}
}
拐弯抹角,我有点绝望,谢谢你的回答,我希望我说清楚了,抱歉我的英语不好
在您的 addDepartement
函数中(在其他地方也有类似的命名...)
$this->departement[] = $departement;
还有包含检查,还有初始化
不过你的 属性 名字很不一样。
private $departements; // <-- plural s
因为 $this->departement
是在构造函数中初始化的,php 会愉快地使用它,而 doctrine 没有注意到这个非托管的任何差异 属性