I am working on a small project to manage the pay slips of a small enterprise with Symfony4 and I use easyAdmin as a beginner. I have three classes (Employe, Lot and Bulletin) linked by ManyToOne - OneToMany relationships to say that an "Employe" has multiple "Bulletin", a "Lot" can have multiple Bulletin and a "Bulletin" belongs to a single "Lot" and a single "Employe": The "Employe" class:
<?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();
}
}
I would now like that when creating a "Lot" with EasyAdmin that a "Bulletin" be generated for each employee and saved in the database. for example if I have 5 employees in my database, when creating a "Lot", 5 bulletins containing the same object Lot + an object "Employe" in the list of 5 are created automatically.
What I have done :
I created a LotController Controller which extends the EasyAdminController class and which I call as the basic controller in my easy_admin.yml
Code:
Easy_admin.yml
Lot: class: App\Entity\Lot controller: 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);
}
}
But when I save a lot only the "Bulletin" of the last "Employe" of my database is generated and I do not know why the first ones are omitted?
If you can help me ... Thank you in advance !
excuse my english, i'm french lol