Symfony 4 - 类对象无法转换为字符串。

8
在网上和这里查找后,我没有找到对我有用的东西。我正在创建一个CRUD。每个“企业”都有一个或多个“站点”,我目前正在为“站点”创建CRUD。我通过执行make:form命令来完成它。当我要创建一个站点时,会出现以下错误:

可捕获致命错误:无法将App\Entity\Entreprise类的对象转换为字符串

我尝试添加函数__toString(),但可能我没有正确添加它,它没有改变任何东西,所以我将其删除了。
我的创建站点的控制器如下:
        /**
         * @Route("admin/sites/new", name="admin.sites.new")
         * @param Request $request
         * @return RedirectResponse|Response
         */
        public function new (Request $request)
        {
            $site = new Site();
            $form = $this->createForm(SiteType::class, $site);
            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()){
                $this->em->persist($site);
                $this->em->flush();
                $this->addFlash('success', 'Site crée avec succès');
                return $this->redirectToRoute('admin.sites.index');
            }
            return $this->render('admin/sites/create.html.twig', [
                'site' => $site,
                'form' => $form->createView()
            ]);
        }
    }

我使用make:form命令生成了我的网站类型:

        /**
         * @Route("admin/sites/new", name="admin.sites.new")
         * @param Request $request
         * @return RedirectResponse|Response
         */
        public function new (Request $request)
        {
            $site = new Site();
            $form = $this->createForm(SiteType::class, $site);
            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()){
                $this->em->persist($site);
                $this->em->flush();
                $this->addFlash('success', 'Site crée avec succès');
                return $this->redirectToRoute('admin.sites.index');
            }
            return $this->render('admin/sites/create.html.twig', [
                'site' => $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;
    }


}

And here is

Site


namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\SiteRepository")
*/
class Site
{
  /**
   * @ORM\Id()
   * @ORM\GeneratedValue()
   * @ORM\Column(type="integer")
   */
  private $id;

  /**
   * @ORM\Column(type="string", length=255)
   */
  private $site_nom;

  /**
   * @ORM\Column(type="string", length=255)
   */
  private $site_raison_sociale;

  /**
   * @ORM\Column(type="string", length=255)
   */
  private $site_APE;

  /**
   * @ORM\ManyToMany(targetEntity="App\Entity\Client", mappedBy="site_id")
   */
  private $site_id;

  /**
   * @ORM\OneToOne(targetEntity="App\Entity\Adresse", cascade={"persist", "remove"})
   * @ORM\JoinColumn(nullable=false)
   */
  private $adresse_id;

  /**
   * @ORM\ManyToOne(targetEntity="App\Entity\Entreprise", inversedBy="entreprise_id")
   * @ORM\JoinColumn(nullable=false)
   */
  private $entreprise_id;

  public function __construct()
  {
      $this->site_id = new ArrayCollection();
  }

  public function getId(): ?int
  {
      return $this->id;
  }

  public function getSiteNom(): ?string
  {
      return $this->site_nom;
  }

  public function setSiteNom(string $site_nom): self
  {
      $this->site_nom = $site_nom;

      return $this;
  }

  public function getSiteRaisonSociale(): ?string
  {
      return $this->site_raison_sociale;
  }

  public function setSiteRaisonSociale(string $site_raison_sociale): self
  {
      $this->site_raison_sociale = $site_raison_sociale;

      return $this;
  }

  public function getSiteAPE(): ?string
  {
      return $this->site_APE;
  }

  public function setSiteAPE(string $site_APE): self
  {
      $this->site_APE = $site_APE;

      return $this;
  }

  /**
   * @return Collection|Client[]
   */
  public function getSiteId(): Collection
  {
      return $this->site_id;
  }

  public function addSiteId(Client $siteId): self
  {
      if (!$this->site_id->contains($siteId)) {
          $this->site_id[] = $siteId;
          $siteId->addSiteId($this);
      }

      return $this;
  }

  public function removeSiteId(Client $siteId): self
  {
      if ($this->site_id->contains($siteId)) {
          $this->site_id->removeElement($siteId);
          $siteId->removeSiteId($this);
      }

      return $this;
  }

  public function getAdresseId(): ?Adresse
  {
      return $this->adresse_id;
  }

  public function setAdresseId(Adresse $adresse_id): self
  {
      $this->adresse_id = $adresse_id;

      return $this;
  }

  public function getEntrepriseId(): ?Entreprise
  {
      return $this->entreprise_id;
  }

  public function setEntrepriseId(?Entreprise $entreprise_id): self
  {
      $this->entreprise_id = $entreprise_id;

      return $this;
  }

  public function __toString()
  {
      return $this->getSiteNom();
  }
}

我不知道问题在哪里。也许是因为我没有正确编写__toString函数!我的代码如下:

public function __toString()
    {
        return $this->getSiteNom();
    }
}

3
__toString() 方法需要存在于你尝试转换为字符串的实体上。 - Gert de Pagter
哪一行导致了那个错误?你想用那段代码实现什么?你的表单类和模板长什么样子? - Nico Haase
1
好的,就像@GertdePagter说的那样,我没有在goof类上使用__toString函数...我是在我的Site实体而不是我的Entreprise中使用它...现在它可以工作了。 - NoobieNoob
1个回答

22
捕获致命错误: 类型为App\Entity\Entreprise的对象
需要在Entreprise实体中实现__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;
    }

    // ...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接