Я создаю приложение для управления багажом и рейсами с Symfony 4, я создал отношение " ManyToMany " между этими объектами.
Когда я хочу добавить новый багаж с пунктом назначения(путешествие) У меня есть эта ошибка:
У меня addVoyage и removeVoyage в моих классах Voyage и Bagage.
Вы найдете над моими классами Bagage.php , мою форму BagageType.php и мой контроллер BagageController.php
Bagage.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\BagageRepository")
*/
class Bagage
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
* @Assert\Length(min=5, max=50)
*/
private $nom;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $objets = [];
/**
* @ORM\Column(type="datetime")
*/
private $dateCreation;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Voyage", mappedBy="bagages")
*/
private $voyages;
public function __construct()
{
$this->voyages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getObjets(): ?array
{
return $this->objets;
}
public function setObjets(?array $objets): self
{
$this->objets = $objets;
return $this;
}
public function getDateCreation(): ?\DateTimeInterface
{
return $this->dateCreation;
}
public function setDateCreation(\DateTimeInterface $dateCreation): self
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* @return Collection|Voyage[]
*/
public function getVoyages(): Collection
{
return $this->voyages;
}
public function addVoyage(Voyage $voyage): self
{
if (!$this->voyages->contains($voyage)) {
$this->voyages[] = $voyage;
$voyage->addBagage($this);
}
return $this;
}
public function removeVoyage(Voyage $voyage): self
{
if ($this->voyages->contains($voyage)) {
$this->voyages->removeElement($voyage);
$voyage->removeBagage($this);
}
return $this;
}
}
?>
BagageType.php
<?php
namespace App\Form;
use App\Entity\Voyage;
use App\Entity\Bagage;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BagageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('voyages', EntityType::class, [
'class' => Voyage::class,
'choice_label' => 'lieu'
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Bagage::class,
]);
}
}
?>
BagageController.php
<?php
/**
* @Route("/bagages/nouveau", name="bagage_creation")
* @Route("/bagages/{id}/edit", name="bagage_edit")
*/
public function form(Bagage $bagage = null, Request $request, ObjectManager $manager)
{
if(!$bagage){
$bagage = new Bagage();
}
$form = $this->createForm(BagageType::class, $bagage);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
if(!$bagage->getId()){
$bagage->setDateCreation(new \Datetime());
}
$manager->persist($bagage);
$manager->flush();
return $this->redirectToRoute('bagage_show', [
'id' => $bagage->getId()
]);
}
return $this->render('bagages/create.html.twig', [
//creation d'une vue pour twig pour afficher le formulaire
'formBagage' => $form->createView(),
'editMode' => $bagage->getId() !== null
]);
}
?>
Не пропущен ли вызов addVoyage в моем контроллере?
EDIT
Voyage.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\VoyageRepository")
*/
class Voyage
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $lieu;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Bagage", inversedBy="voyages")
*/
private $bagages;
public function __construct()
{
$this->bagages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLieu(): ?string
{
return $this->lieu;
}
public function setLieu(string $lieu): self
{
$this->lieu = $lieu;
return $this;
}
/**
* @return Collection|Bagage[]
*/
public function getBagages(): Collection
{
return $this->bagages;
}
public function addBagage(Bagage $bagage): self
{
if (!$this->bagages->contains($bagage)) {
$this->bagages[] = $bagage;
}
return $this;
}
public function removeBagage(Bagage $bagage): self
{
if ($this->bagages->contains($bagage)) {
$this->bagages->removeElement($bagage);
}
return $this;
}
}