#php #symfony5
Вопрос:
это мои проблемы:
сообщение об ошибке:
Expected argument of type "AppEntityMark or null", "instance of AppEntityVoiture" given at property path "mark".
это мои сущности:
метка сущности:
lt;?php namespace AppEntity; use AppRepositoryMarkRepository; use DoctrineCommonCollectionsArrayCollection; use DoctrineCommonCollectionsCollection; use DoctrineORMMapping as ORM; /** * @ORMEntity(repositoryClass=MarkRepository::class) */ class Mark { /** * @ORMId * @ORMGeneratedValue * @ORMColumn(type="integer") */ private $id; /** * @ORMColumn(type="string", length=180) */ private $nameMark; /** * @ORMOneToMany(targetEntity=Voiture::class, mappedBy="mark", orphanRemoval=true) */ private $voiture; public function __construct() { $this-gt;voiture = new ArrayCollection(); } public function getId(): ?int { return $this-gt;id; } public function getNameMark(): ?string { return $this-gt;nameMark; } public function setNameMark(string $nameMark): self { $this-gt;nameMark = $nameMark; return $this; } /** * @return Collection|Voiture[] */ public function getVoiture(): Collection { return $this-gt;voiture; } public function addVoiture(Voiture $voiture): self { if (!$this-gt;voiture-gt;contains($voiture)) { $this-gt;voiture[] = $voiture; $voiture-gt;setMark($this); } return $this; } //ajout method toString pour afficher mes marques public function __toString() { return (string) $this -gt; getNameMark(); } public function removeVoiture(Voiture $voiture): self { if ($this-gt;voiture-gt;removeElement($voiture)) { // set the owning side to null (unless already changed) if ($voiture-gt;getMark() === $this) { $voiture-gt;setMark(null); } } return $this; } }
это моя сущность Voiture:
lt;?php namespace AppEntity; use AppRepositoryVoitureRepository; use DoctrineCommonCollectionsArrayCollection; use DoctrineCommonCollectionsCollection; use DoctrineORMMapping as ORM; use DateTimeInterface; /** * @ORMEntity(repositoryClass=VoitureRepository::class) */ class Voiture { /** * @ORMId * @ORMGeneratedValue * @ORMColumn(type="integer") */ private $id; /** * @ORMColumn(type="string", length=100) */ private $model; /** * @ORMColumn(type="string", length=255) */ private $km; /** * @ORMColumn(type="float") */ private $price; /** * @ORMColumn(type="integer") */ private $numbersOwners; /** * @ORMColumn(type="string", length=50) */ private $engineSize; /** * @ORMColumn(type="string", length=50) */ private $powerEngine; /** * @ORMColumn(type="string", length=35) */ private $fuel; /** * @ORMColumn(type="date") */ private $yearOfEntry; /** * @ORMColumn(type="string", length=80) */ private $transmission; /** * @ORMColumn(type="text") */ private $description; /** * @ORMColumn(type="text") */ private $options; /** * @ORMOneToMany(targetEntity=Image::class, mappedBy="voiture", orphanRemoval=true) */ private $images; /** * @ORMManyToOne(targetEntity=Mark::class, inversedBy="voiture") * @ORMJoinColumn(nullable=false) */ private $mark; public function __toString() { return $this -gt; mark; } /** * @ORMColumn(type="string", length=255) */ private $coverImage; public function __construct() { $this-gt;images = new ArrayCollection(); } public function getId(): ?int { return $this-gt;id; } public function getModel(): ?string { return $this-gt;model; } public function setModel(string $model): self { $this-gt;model = $model; return $this; } public function getKm(): ?string { return $this-gt;km; } public function setKm(string $km): self { $this-gt;km = $km; return $this; } public function getPrice(): ?float { return $this-gt;price; } public function setPrice(float $price): self { $this-gt;price = $price; return $this; } public function getNumbersOwners(): ?int { return $this-gt;numbersOwners; } public function setNumbersOwners(int $numbersOwners): self { $this-gt;numbersOwners = $numbersOwners; return $this; } public function getEngineSize(): ?string { return $this-gt;engineSize; } public function setEngineSize(string $engineSize): self { $this-gt;engineSize = $engineSize; return $this; } public function getPowerEngine(): ?string { return $this-gt;powerEngine; } public function setPowerEngine(string $powerEngine): self { $this-gt;powerEngine = $powerEngine; return $this; } public function getFuel(): ?string { return $this-gt;fuel; } public function setFuel(string $fuel): self { $this-gt;fuel = $fuel; return $this; } public function getYearOfEntry(): ?DateTimeInterface { return $this-gt;yearOfEntry; } public function setYearOfEntry(DateTimeInterface $yearOfEntry): self { $this-gt;yearOfEntry = $yearOfEntry; return $this; } public function getTransmission(): ?string { return $this-gt;transmission; } public function setTransmission(string $transmission): self { $this-gt;transmission = $transmission; return $this; } public function getDescription(): ?string { return $this-gt;description; } public function setDescription(string $description): self { $this-gt;description = $description; return $this; } public function getOptions(): ?string { return $this-gt;options; } public function setOptions(string $options): self { $this-gt;options = $options; return $this; } /** * @return Collection|Image[] */ public function getImages(): Collection { return $this-gt;images; } public function addImage(Image $image): self { if (!$this-gt;images-gt;contains($image)) { $this-gt;images[] = $image; $image-gt;setVoiture($this); } return $this; } public function removeImage(Image $image): self { if ($this-gt;images-gt;removeElement($image)) { // set the owning side to null (unless already changed) if ($image-gt;getVoiture() === $this) { $image-gt;setVoiture(null); } } return $this; } public function getMark(): ?Mark { return $this-gt;mark; } public function setMark(?Mark $mark): self { $this-gt;mark = $mark; return $this; } public function getCoverImage(): ?string { return $this-gt;coverImage; } public function setCoverImage(string $coverImage): self { $this-gt;coverImage = $coverImage; return $this; } }
and my formBuilder:
lt;?php namespace AppForm; use AppEntityMark; use AppEntityVoiture; use AppFormImageCarType; use SymfonyComponentFormAbstractType; use SymfonyComponentFormFormBuilderInterface; use SymfonyBridgeDoctrineFormTypeEntityType; use SymfonyComponentOptionsResolverOptionsResolver; use SymfonyComponentFormExtensionCoreTypeUrlType; use SymfonyComponentFormExtensionCoreTypeDateType; use SymfonyComponentFormExtensionCoreTypeTextType; use SymfonyComponentFormExtensionCoreTypeMoneyType; use SymfonyComponentFormExtensionCoreTypeIntegerType; use SymfonyComponentFormExtensionCoreTypeCollectionType; class NewCarType extends AbstractType { /** * function de config pour mes champs * * @param string $label * @param string $placeholder * @param array $options * @return array */ private function getConfiguration($label,$placeholder,$options=[]){ return array_merge_recursive([ 'label' =gt; $label, 'attr' =gt; [ 'placeholder'=gt; $placeholder, 'class' =gt; 'form-control mb-1' ] ],$options); } public function buildForm(FormBuilderInterface $builder, array $options): void { $builder -gt;add('model', TextType::class,$this-gt;getConfiguration('Model de la voiture:','bmw-z')) -gt;add('km', TextType::class, $this-gt; getConfiguration('Kilométrage de la voiture:','42000')) -gt;add('price',MoneyType::class, $this-gt; getConfiguration('Prix de la voiture:','5500')) -gt;add('numbersOwners',IntegerType::class,$this-gt; getConfiguration('Nombre de propriétaire:','0')) -gt;add('engineSize',IntegerType::class,$this-gt; getConfiguration('Cylindrée:','400')) -gt;add('powerEngine',IntegerType::class,$this-gt; getConfiguration('Puissance du moteur:','200')) -gt;add('fuel',TextType::class, $this-gt; getConfiguration('Carburant:','diesel/Essence')) -gt;add('yearOfEntry',DateType::class, $this-gt;getConfiguration('Date de mise en circulation:',false)) -gt;add('transmission',TextType::class, $this-gt; getConfiguration('Transmission:','Automatique/Manuelle')) -gt;add('description',TextType::class, $this-gt; getConfiguration('Description du véhicule:','description véhicule..')) -gt;add('options',TextType::class, $this-gt; getConfiguration('Options du véhicule:','Gps,cruise-control...')) -gt;add('coverImage',UrlType::class, $this-gt; getConfiguration('Ajouter une url d'image:','https://picsum.photos/200/300')) -gt;add('mark',EntityType::class, $this-gt;getConfiguration('Choix de la marque:',false,[ 'class' =gt; Voiture::class, 'choice_label' =gt; 'mark' ])) -gt;add('images',CollectionType::class,[ 'entry_type' =gt; ImageCarType::class, 'allow_add' =gt; true, 'allow_delete' =gt; true ]) ; } public function configureOptions(OptionsResolver $resolver): void { $resolver-gt;setDefaults([ 'data_class' =gt; Voiture::class, ]); } }
my Controller newcar:
public function createCar(Request $request, EntityManagerInterface $manager){
$carNew = new Voiture();
// on met deux images pour voir si tout c’est bien passé quand et voir si ça m’affiche bien $title = $request-gt;request-gt;get(‘addVoiture’); dump($title);
$image1 = new Image(); $image1-gt;setNameImg('http://placehold.it/400x200') -gt;setCaption('Titre 1'); $carNew-gt;addImage($image1); $image2 = new Image(); $image2-gt;setNameImg('http://placehold.it/400x200') -gt;setCaption('Titre 2'); $carNew-gt;addImage($image2); $form = $this-gt;createForm(NewCarType::class,$carNew); $form -gt;handleRequest($request); // quand je vais vérifier si le form est envoyé et si il est valide if($form -gt; isSubmitted() amp;amp; $form -gt; isValid()){ foreach($carNew-gt;getImages() as $image){ $image-gt;setVoiture($carNew); $manager-gt;persist($image); } $manager-gt;flush(); return $this-gt;render('show_room/{id}.html.twig'); } return $this-gt;render('show_room/newcar.html.twig',[ 'carForm' =gt; $form-gt;createView() ]); }
где же проблемы?