Symfony 4 EasyAdmin捆绑创建由ManyToMany关系链接的实体

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

我正在使用Symfony4来管理一个小型企业的工资单的小项目,我以easyAdmin作为初学者。我有3个类(Employe,Lot和Bulletin),由ManyToOne链接-OneToMany关系是说一个“雇员”具有多个“公告”,一个“批次”可以具有多个公告,一个“公告”属于一个“批次”和一个“雇员”:“雇员”类:

<?php

class Employe
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
      /**
     * @ORM\Column(type="string", length=255)
     */
    private $fullName;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Bulletin", mappedBy="employe", orphanRemoval=true)
     */
    private $bulletins;

    public function __construct()
    {
        $this->elements = new ArrayCollection();
        $this->liaisons = new ArrayCollection();
        $this->bulletins = new ArrayCollection();
    }
    /**
     * @return Collection|Bulletin[]
     */
    public function getBulletins(): Collection
    {
        return $this->bulletins;
    }

    public function addBulletin(Bulletin $bulletin): self
    {
        if (!$this->bulletins->contains($bulletin)) {
            $this->bulletins[] = $bulletin;
            $bulletin->setEmploye($this);
        }

        return $this;
    }

    public function removeBulletin(Bulletin $bulletin): self
    {
        if ($this->bulletins->contains($bulletin)) {
            $this->bulletins->removeElement($bulletin);
            // set the owning side to null (unless already changed)
            if ($bulletin->getEmploye() === $this) {
                $bulletin->setEmploye(null);
            }
        }

        return $this;
    }
    // getters and setters
}

//La classe Lot :

class Lot
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Bulletin", mappedBy="lot", orphanRemoval=true, cascade={"persist"})
     */
    private $bulletins;

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

    }

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

    public function getLibelle(): ?string
    {
        return $this->libelle;
    }

    public function setLibelle(string $libelle): self
    {
        $this->libelle = $libelle;

        return $this;
    }
    /**
     * @return Collection|Bulletin[]
     */
    public function getBulletins(): Collection
    {
        return $this->bulletins;
    }

    public function addBulletin(Bulletin $bulletin): self
    {
        if (!$this->bulletins->contains($bulletin)) {
            $this->bulletins[] = $bulletin;
            $bulletin->setLot($this);
        }

        return $this;
    }

    public function removeBulletin(Bulletin $bulletin): self
    {
        if ($this->bulletins->contains($bulletin)) {
            $this->bulletins->removeElement($bulletin);
            // set the owning side to null (unless already changed)
            if ($bulletin->getLot() === $this) {
                $bulletin->setLot(null);
            }
        }

        return $this;
    }

    public function __toString(){

        return $this->libelle;
    }
}


//La classe bulletin

class Bulletin
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Employe", inversedBy="bulletins")
     * @ORM\JoinColumn(nullable=false)
     */
    private $employe;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Lot", inversedBy="bulletins",cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $lot;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Figurer", mappedBy="bulletin", orphanRemoval=true)
     */
    private $figurations;

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

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

    public function getEmploye(): ?Employe
    {
        return $this->employe;
    }

    public function setEmploye(?Employe $employe): self
    {
        $this->employe = $employe;

        return $this;
    }

    public function getLot(): ?Lot
    {
        return $this->lot;
    }

    public function setLot(?Lot $lot): self
    {
        $this->lot = $lot;

        return $this;
    }

    /**
     * @return Collection|Figurer[]
     */
    public function getFigurations(): Collection
    {
        return $this->figurations;
    }

    public function addFiguration(Figurer $figuration): self
    {
        if (!$this->figurations->contains($figuration)) {
            $this->figurations[] = $figuration;
            $figuration->setBulletin($this);
        }

        return $this;
    }

    public function removeFiguration(Figurer $figuration): self
    {
        if ($this->figurations->contains($figuration)) {
            $this->figurations->removeElement($figuration);
            // set the owning side to null (unless already changed)
            if ($figuration->getBulletin() === $this) {
                $figuration->setBulletin(null);
            }
        }

        return $this;
    }

    public function __toString(){

        return $this->employe->getId().' '.$this->employe->getFullName();
    }
}

我现在希望在使用EasyAdmin创建“批次”时为每个员工生成一个“公告”并将其保存在数据库中。例如,如果我的数据库中有5名员工,则在创建“批次”时,将自动创建5个包含相同对象Lot的公告,并在5个列表中自动创建对象“ Employe”。]

我做了什么:

我创建了一个LotController Controller,它扩展了EasyAdminController类,并在easy_admin.yml中将其称为基本控制器

代码:

Easy_admin.yml

全部:类别:App \ Entity \ Lot控制器:App \ Controller \ LotController

LotController.php

<?php

namespace App\Controller;

use App\Entity\Employe;
use App\Entity\Bulletin;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;

class LotController extends EasyAdminController
{
    public function persistEntity($entity)
    {

        $entityManager = $this->getDoctrine()->getManager();

        $employes =  $this->getDoctrine()
        ->getRepository(Employe::class)
        ->findAll();

        $bulletin = new Bulletin();

        $bulletin->setLot($entity);

        foreach($employes as $employe) {

            $bulletin->setEmploye($employe);
            $entityManager->persist($bulletin);
            $entityManager->flush();
        }

        parent::persistEntity($entity);   
    }
}

但是当我节省很多时,只会生成数据库中最后一个“雇员”的“公告”,我不知道为什么省略了第一个?”>

如果您能帮助我...预先感谢您!

对不起,我是法国人,哈哈

我正在使用Symfony4来管理一个小型企业的工资单的小项目,我以easyAdmin作为初学者。我有通过ManyToOne链接的三个类(员工,地段和公告)-...

controller symfony4 relation many-to-one easyadmin
1个回答
0
投票

还是同样的问题!请帮帮我 !我尝试了许多其他事情,但还是一无所获。

© www.soinside.com 2019 - 2024. All rights reserved.