Symfony и easyadmin: как работать с зависимыми объектами?

Я использую easyadmin с сущностью «Клиент», у которой есть поле, связанное с другой сущностью «Страна». Мне удалось создать форму администратора, добавив в объект Country функцию __toString (), чтобы избежать ошибки

«Объект класса App Entity Country не может быть преобразован в строку»</p>

.

Теперь, когда я пытаюсь сохранить эту запись, я получаю сообщение об ошибке:

Ожидаемое значение типа «Приложение Сущность Страна» для поля связи «Приложение Сущность Клиент # $ country».

Пожалуйста, помогите!

Редактировать :

Приложение Сущность Клиент:

<?php namespace AppEntity; use DoctrineORMMapping as ORM; use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity; use GedmoTimestampableTraitsTimestampableEntity; use GedmoSoftDeleteableTraitsSoftDeleteableEntity; use GedmoMappingAnnotation as Gedmo; /** * @ORMEntity(repositoryClass=»AppRepositoryClientRepository») * @GedmoSoftDeleteable() * @UniqueEntity(fields={«companyname», «firstname», «lastname», «email»}, message=»There is already an account with this username») */ class Client { use TimestampableEntity; use SoftDeleteableEntity; /** * @ORMId() * @ORMGeneratedValue() * @ORMColumn(type=»integer») */ private $id; /** * @ORMColumn(type=»string», length=50) */ private $companyname; … /** * @ORMManyToOne(targetEntity=»AppEntityCountry») * @ORMJoinColumn(nullable=false) */ private $country; /** * @ORMManyToOne(targetEntity=»AppEntityState») * @ORMJoinColumn(nullable=false) */ private $stateprov; public function getId(): ?int { return $this->id; } public function getCompanyname(): ?string { return $this->companyname; } public function setCompanyname(string $companyname): self { $this->companyname = $companyname; return $this; } … public function getCountry(): ?string { return $this->country; } public function setCountry(string $country): self { $this->country = $country; return $this; } public function getStateprov(): ?state { return $this->stateprov; } public function setStateprov(?state $stateprov): self { $this->stateprov = $stateprov; return $this; } }

Приложение Сущность Страна

<?php namespace AppEntity; use DoctrineCommonCollectionsArrayCollection; use DoctrineCommonCollectionsCollection; use DoctrineORMMapping as ORM; use GedmoTimestampableTraitsTimestampableEntity; use GedmoSoftDeleteableTraitsSoftDeleteableEntity; use GedmoMappingAnnotation as Gedmo; /** * @ORMEntity(repositoryClass=»AppRepositoryCountryRepository») * @GedmoSoftDeleteable() */ class Country { use TimestampableEntity; use SoftDeleteableEntity; /** * @ORMId() * @ORMGeneratedValue() * @ORMColumn(type=»integer») */ private $id; /** * @ORMColumn(type=»string», length=100) */ private $name; /** * @ORMColumn(type=»string», length=3, nullable=true) */ private $iso3; /** * @ORMColumn(type=»string», length=2, nullable=true) */ private $iso2; /** * @ORMColumn(type=»string», length=255, nullable=true) */ private $phonecode; /** * @ORMColumn(type=»string», length=255, nullable=true) */ private $capital; /** * @ORMColumn(type=»string», length=255, nullable=true) */ private $currency; /** * @ORMColumn(type=»smallint») */ private $flag; /** * @ORMOneToMany(targetEntity=»AppEntityState», mappedBy=»country») */ private $states; public function __construct() { $this->states = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getName(): ?string { return $this->name; } public function setName(string $name): self { $this->name = $name; return $this; } public function getIso3(): ?string { return $this->iso3; } public function setIso3(?string $iso3): self { $this->iso3 = $iso3; return $this; } public function getIso2(): ?string { return $this->iso2; } public function setIso2(?string $iso2): self { $this->iso2 = $iso2; return $this; } public function getPhonecode(): ?string { return $this->phonecode; } public function setPhonecode(?string $phonecode): self { $this->phonecode = $phonecode; return $this; } public function getCapital(): ?string { return $this->capital; } public function setCapital(?string $capital): self { $this->capital = $capital; return $this; } public function getCurrency(): ?string { return $this->currency; } public function setCurrency(?string $currency): self { $this->currency = $currency; return $this; } public function getFlag(): ?int { return $this->flag; } public function setFlag(int $flag): self { $this->flag = $flag; return $this; } /** * @return Collection|State[] */ public function getStates(): Collection { return $this->states; } public function addState(State $state): self { if (!$this->states->contains($state)) { $this->states[] = $state; $state->setCountryId($this); } return $this; } public function removeState(State $state): self { if ($this->states->contains($state)) { $this->states->removeElement($state); // set the owning side to null (unless already changed) if ($state->getCountryId() === $this) { $state->setCountryId(null); } } return $this; } public function __toString(){ // to show the name of the Category in the select return $this->name; // to show the id of the Category in the select // return $this->id; } }

Приложение Сущность Состояние

<?php namespace AppEntity; use DoctrineORMMapping as ORM; use GedmoTimestampableTraitsTimestampableEntity; use GedmoSoftDeleteableTraitsSoftDeleteableEntity; use GedmoMappingAnnotation as Gedmo; /** * @ORMEntity(repositoryClass=»AppRepositoryStateRepository») * @GedmoSoftDeleteable() */ class State { use TimestampableEntity; use SoftDeleteableEntity; /** * @ORMId() * @ORMGeneratedValue() * @ORMColumn(type=»integer») */ private $id; /** * @ORMColumn(type=»string», length=255) */ private $name; /** * @ORMColumn(type=»smallint») */ private $flag; /** * @ORMManyToOne(targetEntity=»AppEntitycountry», inversedBy=»states») * @ORMJoinColumn(nullable=false) */ private $country; public function getId(): ?int { return $this->id; } public function getName(): ?string { return $this->name; } public function setName(string $name): self { $this->name = $name; return $this; } public function getFlag(): ?int { return $this->flag; } public function setFlag(int $flag): self { $this->flag = $flag; return $this; } public function getCountry(): ?country { return $this->country; } public function setCountry(?country $country): self { $this->country = $country; return $this; } public function __toString(){ // to show the name of the Category in the select return $this->name; // to show the id of the Category in the select // return $this->id; } }

можешь показать свои Entities оба?   —  person Alexglvr    schedule 24.10.2019

пожалуйста, посмотрите правки   —  person Alexglvr    schedule 24.10.2019

Источник: ledsshop.ru

Стиль жизни - Здоровье!