我将easyadmin与实体"Client"一起使用,该实体有一个链接到另一个实体"Country"的字段。我设法制作了管理表单,通过向Country实体添加__toString((函数来避免错误
"App\Entity\Country类的对象无法转换为字符串">
。
现在,当我试图保存条目时,我得到了错误:
关联字段的类型为"App\Entity\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;
}
}
Client
中删除?string
。因为getCountry返回Country
类。
class Client {
.......................
public function getCountry():
{
return $this->country;
}