Php Symfony 4-类的对象无法转换为字符串
在浏览了互联网之后,我没有发现对我有用的东西。 我目前正在创建一个积垢。每个“企业”都有一个或多个“站点”,我目前正在为站点做CRUD。我是通过执行Php Symfony 4-类的对象无法转换为字符串,php,symfony,object,Php,Symfony,Object,在浏览了互联网之后,我没有发现对我有用的东西。 我目前正在创建一个积垢。每个“企业”都有一个或多个“站点”,我目前正在为站点做CRUD。我是通过执行make:form命令实现的。 当我要创建站点时,会出现以下错误: 可捕获的致命错误:类App\Entity\enterprise的对象无法捕获 转换为字符串 正如我所看到的,我已经尝试添加函数\uu toString()。但也许我没有正确地添加它,因为它没有改变任何东西,所以我删除了它 用于创建站点的控制器如下所示: /** *@Route(“ad
make:form
命令实现的。
当我要创建站点时,会出现以下错误:
可捕获的致命错误:类App\Entity\enterprise的对象无法捕获
转换为字符串
正如我所看到的,我已经尝试添加函数\uu toString()
。但也许我没有正确地添加它,因为它没有改变任何东西,所以我删除了它
用于创建站点的控制器如下所示:
/**
*@Route(“admin/sites/new”,name=“admin.sites.new”)
*@param Request$Request
*@return-Response | Response
*/
新公共功能(请求$Request)
{
$site=新站点();
$form=$this->createForm(SiteType::class,$site);
$form->handleRequest($request);
如果($form->isSubmitted()&&&$form->isValid()){
$this->em->persist($site);
$this->em->flush();
$this->addFlash('success','Site crée avec success');
返回$this->redirectToRoute('admin.sites.index');
}
返回$this->render('admin/sites/create.html.twig'[
“站点”=>$site,
'form'=>$form->createView()
]);
}
}
我的站点类型由make:form
命令生成:
/**
*@Route(“admin/sites/new”,name=“admin.sites.new”)
*@param Request$Request
*@return-Response | Response
*/
新公共功能(请求$Request)
{
$site=新站点();
$form=$this->createForm(SiteType::class,$site);
$form->handleRequest($request);
如果($form->isSubmitted()&&&$form->isValid()){
$this->em->persist($site);
$this->em->flush();
$this->addFlash('success','Site crée avec success');
返回$this->redirectToRoute('admin.sites.index');
}
返回$this->render('admin/sites/create.html.twig'[
“站点”=>$site,
'form'=>$form->createView()
]);
}
}
这是我的实体
企业
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\EntrepriseRepository")
*/
class Entreprise
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_nom;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_siret;
/**
* @ORM\Column(type="string", length=10)
*/
private $entreprise_telephone;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_salesforce_number;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_compte_client;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_raison_sociale;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_APE;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $entreprise_image_link;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Site", mappedBy="entreprise_id")
*/
private $entreprise_id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Catalogue", mappedBy="entreprise_id")
*/
private $entreprise_catalogue_id;
public function __construct()
{
$this->entreprise_id = new ArrayCollection();
$this->entreprise_catalogue_id = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEntrepriseNom(): ?string
{
return $this->entreprise_nom;
}
public function setEntrepriseNom(string $entreprise_nom): self
{
$this->entreprise_nom = $entreprise_nom;
return $this;
}
public function getEntrepriseSiret(): ?string
{
return $this->entreprise_siret;
}
public function setEntrepriseSiret(string $entreprise_siret): self
{
$this->entreprise_siret = $entreprise_siret;
return $this;
}
public function getEntrepriseTelephone(): ?int
{
return $this->entreprise_telephone;
}
public function setEntrepriseTelephone(int $entreprise_telephone): self
{
$this->entreprise_telephone = $entreprise_telephone;
return $this;
}
public function getEntrepriseSalesforceNumber(): ?string
{
return $this->entreprise_salesforce_number;
}
public function setEntrepriseSalesforceNumber(string $entreprise_salesforce_number): self
{
$this->entreprise_salesforce_number = $entreprise_salesforce_number;
return $this;
}
public function getEntrepriseCompteClient(): ?string
{
return $this->entreprise_compte_client;
}
public function setEntrepriseCompteClient(string $entreprise_compte_client): self
{
$this->entreprise_compte_client = $entreprise_compte_client;
return $this;
}
public function getEntrepriseRaisonSociale(): ?string
{
return $this->entreprise_raison_sociale;
}
public function setEntrepriseRaisonSociale(string $entreprise_raison_sociale): self
{
$this->entreprise_raison_sociale = $entreprise_raison_sociale;
return $this;
}
public function getEntrepriseAPE(): ?string
{
return $this->entreprise_APE;
}
public function setEntrepriseAPE(string $entreprise_APE): self
{
$this->entreprise_APE = $entreprise_APE;
return $this;
}
public function getEntrepriseImageLink(): ?string
{
return $this->entreprise_image_link;
}
public function setEntrepriseImageLink(?string $entreprise_image_link): self
{
$this->entreprise_image_link = $entreprise_image_link;
return $this;
}
/**
* @return Collection|Site[]
*/
public function getEntrepriseId(): Collection
{
return $this->entreprise_id;
}
public function addEntrepriseId(Site $entrepriseId): self
{
if (!$this->entreprise_id->contains($entrepriseId)) {
$this->entreprise_id[] = $entrepriseId;
$entrepriseId->setEntrepriseId($this);
}
return $this;
}
public function removeEntrepriseId(Site $entrepriseId): self
{
if ($this->entreprise_id->contains($entrepriseId)) {
$this->entreprise_id->removeElement($entrepriseId);
// set the owning side to null (unless already changed)
if ($entrepriseId->getEntrepriseId() === $this) {
$entrepriseId->setEntrepriseId(null);
}
}
return $this;
}
}
这是
站点
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\EntrepriseRepository")
*/
class Entreprise
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_nom;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_siret;
/**
* @ORM\Column(type="string", length=10)
*/
private $entreprise_telephone;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_salesforce_number;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_compte_client;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_raison_sociale;
/**
* @ORM\Column(type="string", length=255)
*/
private $entreprise_APE;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $entreprise_image_link;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Site", mappedBy="entreprise_id")
*/
private $entreprise_id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Catalogue", mappedBy="entreprise_id")
*/
private $entreprise_catalogue_id;
public function __construct()
{
$this->entreprise_id = new ArrayCollection();
$this->entreprise_catalogue_id = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEntrepriseNom(): ?string
{
return $this->entreprise_nom;
}
public function setEntrepriseNom(string $entreprise_nom): self
{
$this->entreprise_nom = $entreprise_nom;
return $this;
}
public function getEntrepriseSiret(): ?string
{
return $this->entreprise_siret;
}
public function setEntrepriseSiret(string $entreprise_siret): self
{
$this->entreprise_siret = $entreprise_siret;
return $this;
}
public function getEntrepriseTelephone(): ?int
{
return $this->entreprise_telephone;
}
public function setEntrepriseTelephone(int $entreprise_telephone): self
{
$this->entreprise_telephone = $entreprise_telephone;
return $this;
}
public function getEntrepriseSalesforceNumber(): ?string
{
return $this->entreprise_salesforce_number;
}
public function setEntrepriseSalesforceNumber(string $entreprise_salesforce_number): self
{
$this->entreprise_salesforce_number = $entreprise_salesforce_number;
return $this;
}
public function getEntrepriseCompteClient(): ?string
{
return $this->entreprise_compte_client;
}
public function setEntrepriseCompteClient(string $entreprise_compte_client): self
{
$this->entreprise_compte_client = $entreprise_compte_client;
return $this;
}
public function getEntrepriseRaisonSociale(): ?string
{
return $this->entreprise_raison_sociale;
}
public function setEntrepriseRaisonSociale(string $entreprise_raison_sociale): self
{
$this->entreprise_raison_sociale = $entreprise_raison_sociale;
return $this;
}
public function getEntrepriseAPE(): ?string
{
return $this->entreprise_APE;
}
public function setEntrepriseAPE(string $entreprise_APE): self
{
$this->entreprise_APE = $entreprise_APE;
return $this;
}
public function getEntrepriseImageLink(): ?string
{
return $this->entreprise_image_link;
}
public function setEntrepriseImageLink(?string $entreprise_image_link): self
{
$this->entreprise_image_link = $entreprise_image_link;
return $this;
}
/**
* @return Collection|Site[]
*/
public function getEntrepriseId(): Collection
{
return $this->entreprise_id;
}
public function addEntrepriseId(Site $entrepriseId): self
{
if (!$this->entreprise_id->contains($entrepriseId)) {
$this->entreprise_id[] = $entrepriseId;
$entrepriseId->setEntrepriseId($this);
}
return $this;
}
public function removeEntrepriseId(Site $entrepriseId): self
{
if ($this->entreprise_id->contains($entrepriseId)) {
$this->entreprise_id->removeElement($entrepriseId);
// set the owning side to null (unless already changed)
if ($entrepriseId->getEntrepriseId() === $this) {
$entrepriseId->setEntrepriseId(null);
}
}
return $this;
}
}
名称空间App\Entity;
使用条令\Common\Collections\ArrayCollection;
使用条令\公共\集合\集合;
使用条令\ORM\Mapping作为ORM;
/**
*@ORM\Entity(repositoryClass=“App\Repository\sitepository”)
*/
课堂现场
{
/**
*@ORM\Id()
*@ORM\GeneratedValue()
*@ORM\Column(type=“integer”)
*/
私人$id;
/**
*@ORM\Column(type=“string”,长度=255)
*/
私人$site_nom;
/**
*@ORM\Column(type=“string”,长度=255)
*/
私人$site\u reason\u sociale;
/**
*@ORM\Column(type=“string”,长度=255)
*/
私人$site_APE;
/**
*@ORM\ManyToMany(targetEntity=“App\Entity\Client”,mappedBy=“site\u id”)
*/
私人$site\u id;
/**
*@ORM\OneToOne(targetEntity=“App\Entity\Adresse”,cascade={“persist”,“remove”})
*@ORM\JoinColumn(nullable=false)
*/
私人$adresse_id;
/**
*@ORM\ManyToOne(targetEntity=“App\Entity\enterprise”,inversedBy=“enterprise\u id”)
*@ORM\JoinColumn(nullable=false)
*/
私人$Enterprise_id;
公共函数构造()
{
$this->site_id=new ArrayCollection();
}
公共函数getId():?int
{
返回$this->id;
}
公共函数getSiteNom():?字符串
{
返回$this->site\u nom;
}
公共函数setSiteNom(字符串$site_nom):self
{
$this->site\u nom=$site\u nom;
退还$this;
}
公共函数getSiteRaisonSociale():?字符串
{
返回$this->site\u reason\u sociale;
}
公共函数setSiteRaisonSociale(字符串$site\u raison\u sociale):self
{
$this->site\u raisson\u sociale=$site\u raisson\u sociale;
退还$this;
}
公共函数getSiteAPE():?字符串
{
返回$this->site\u APE;
}
公共函数setSiteAPE(字符串$site_APE):self
{
$this->site\u APE=$site\u APE;
退还$this;
}
/**
*@return Collection | Client[]
*/
公共函数getSiteId():集合
{
返回$this->site\u id;
}
公共函数addSiteId(客户端$siteId):self
{
如果(!$this->site\u id->包含($siteId)){
$this->site_id[]=$siteId;
$siteId->addSiteId($this);
}
退还$this;
}
公共函数removeSiteId(客户端$siteId):self
{
如果($this->site\u id->包含($siteId)){
$this->site\u id->removelement($siteId);
$siteId->removeSiteId($this);
}
退还$this;
}
公共函数getAdresseId():?ADRESE
{
返回$this->address_id;
}
公共函数setAdresseId(Adresse$Adresse_id):self
{
$this->adresse\u id=$adresse\u id;
退还$this;
}
公共函数getEnterpriseId():?Enterprise
{
返回$this->enterprise\u id;
}
公共函数setEnterpriseId(?enterprise$enterprise_id):self
{
$this->enterprise\u id=$enterprise\u id;
退还$this;
}
公共函数
{
返回$this->getSiteNom();
}
}
我不知道怎么了。可能是我写的不正确!
我写过:
公共函数
{
返回$this->getSiteNom();
}
}
可捕获的致命错误:类App\Entity\enterprise的对象
您需要在企业实体中实现\uu toString()方法
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\EntrepriseRepository")
*/
class Entreprise
{
//...
public function __toString()
{
return $this->entreprise_nom;
}
// ...
}
您试图转换为字符串的实体上需要存在
\uuu toString()
方法。哪一行导致该错误?你想用它实现什么