Bonjour,
J'essaie de cloner un objet avec des relations OneToMany. Quand je lance le code j'ai bien un clone de créé et les données copiés mais il supprime les données des relations dans l'original. J'essaye de cloner avec la propriété $attaques lvls dans mes tests.
Voici le code des entités et de la fonction dans le controller.
Entité Pokemon
<?php
namespace App\Entity;
use App\Repository\PokemonRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: PokemonRepository::class)]
class Pokemon
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 100)]
private $nom;
#[ORM\Column(type: 'string', length: 100)]
private $categorie;
#[ORM\Column(type: 'integer')]
private $numPokedex;
#[ORM\Column(type: 'float')]
private $taille;
#[ORM\Column(type: 'float')]
private $poids;
#[ORM\Column(type: 'integer')]
private $vita;
#[ORM\Column(type: 'integer')]
private $dex;
#[ORM\Column(type: 'integer')]
private $forc;
#[ORM\Column(type: 'integer')]
private $concentration;
#[ORM\Column(type: 'integer')]
private $endurance;
#[ORM\Column(type: 'integer')]
private $volonte;
#[ORM\Column(type: 'string', length: 255)]
private $image;
#[ORM\Column(type: 'string', length: 255)]
private $miniature;
#[ORM\ManyToOne(targetEntity: Generation::class, inversedBy: 'pokemon')]
#[ORM\JoinColumn(nullable: false)]
private $generation;
#[ORM\ManyToOne(targetEntity: Ratio::class, inversedBy: 'pokemon')]
#[ORM\JoinColumn(nullable: false)]
private $ratio;
#[ORM\ManyToOne(targetEntity: Type::class, inversedBy: 'pokemon')]
#[ORM\JoinColumn(nullable: false)]
private $type;
#[ORM\OneToMany(mappedBy: 'pokemon', targetEntity: PokemonHasTalents::class, orphanRemoval: true, cascade: ['persist'])]
private $pokemonHasTalents;
#[ORM\OneToMany(mappedBy: 'pokemon', targetEntity: AttaquesLvl::class, orphanRemoval: true, cascade: ['persist'])]
private $attaquesLvls;
#[ORM\OneToMany(mappedBy: 'pokemon', targetEntity: AttaquesCT::class, orphanRemoval: true, cascade: ['persist'])]
private $attaquesCTs;
#[ORM\OneToMany(mappedBy: 'pokemon', targetEntity: AttaquesDT::class, orphanRemoval: true, cascade: ['persist'])]
private $attaquesDTs;
#[ORM\OneToMany(mappedBy: 'pokemon', targetEntity: AttaquesEggMoves::class, orphanRemoval: true, cascade: ['persist'])]
private $attaquesEggMoves;
#[ORM\OneToMany(mappedBy: 'pokemon', targetEntity: AttaquesTutorat::class, orphanRemoval: true, cascade: ['persist'])]
private $attaquesTutorats;
#[ORM\Column(type: 'string', length: 100, nullable: true)]
private $maniere;
#[ORM\OneToMany(mappedBy: 'pokemon', targetEntity: Evolutions::class, orphanRemoval: true, cascade: ['persist'])]
private $evolutions;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'pokemon')]
private $preEvolution;
#[ORM\OneToMany(mappedBy: 'preEvolution', targetEntity: self::class, cascade: ['persist'])]
private $pokemon;
#[ORM\OneToMany(mappedBy: 'pokemon', targetEntity: Formes::class, orphanRemoval: true, cascade: ['persist'])]
private $formes;
#[ORM\Column(type: 'text', nullable: true)]
private $description;
public function __clone()
{
if ($this->id) {
$this->id = null;
$this->attaquesLvls = new ArrayCollection();
}
}
//les Getter et Setter
}
Entité AttaquesLvls
#[ORM\Entity(repositoryClass: AttaquesLvlRepository::class)]
class AttaquesLvl
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\ManyToOne(targetEntity: Pokemon::class, inversedBy: 'attaquesLvls')]
#[ORM\JoinColumn(nullable: false)]
private $pokemon;
#[ORM\ManyToOne(targetEntity: Attaque::class)]
#[ORM\JoinColumn(nullable: false)]
private $attaque;
#[ORM\Column(type: 'string', length: 10)]
private $lvl;
//les Getter et Setter
}
La fonction
#[Route('/duplicate/{id}', name: 'app_pokemon_dupplicate', methods: ['GET', 'POST'])]
public function duplicate(EntityManagerInterface $manager, Pokemon $pokemon): Response
{
$attaquesLvls = $pokemon->getAttaquesLvls();
$newpokemon = clone $pokemon;
$manager->persist($newpokemon);
$manager->flush();
foreach ($attaquesLvls as $attaqueLvl) {
$newpokemon->addAttaquesLvl($attaqueLvl);
}
$manager->persist($newpokemon);
$manager->flush();
return $this->redirectToRoute('app_pokemon_index', [], Response::HTTP_SEE_OTHER);
}
Ce que je veux
Cloner l'objet sans suppression de données
Ce que j'obtiens
Les AttaquesLvls sont supprimés du Pokemon originel
Merci par avance
Sixale