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

标签 php symfony object

在浏览互联网和此处后,我没有找到对我有用的东西。 我目前正在创建一个 CRUD。每个“企业”都有一个或多个“站点”,我目前正在为站点做 CRUD。我是通过执行 make:form 命令来实现的。 当我要创建一个站点时出现以下错误:

Catchable Fatal Error: Object of class App\Entity\Entreprise could not be converted to string

如我所见,我已尝试添加函数 __toString()。但也许我没有正确地添加它,它没有任何改变,所以我删除了它。

我创建网站的 Controller 如下所示:

        /**
         * @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()
            ]);
        }
    }

我的 SiteType 由 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;
    }


}

这里是

网站


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();
    }
}

最佳答案

Catchable Fatal Error: Object of class 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;
    }

    // ...
}

关于php - Symfony 4 - 类的对象无法转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55948523/

相关文章:

Python 在特定条件下停止嗅探

php - Laravel 5.6 - Eloquent 多对多错误 1066

mysql - 更改表时出现异常

java - 按车牌号比较汽车对象列表

vba - "Object not set"错误

Symfony2 ORM prePersist 不工作

php - 使用 AJAX 执行 PHP 脚本而不使用 POST/GET?

php - 如何使用此分页代码获取页面上的项目数?

php - 将制表符/空格分隔的行转换为嵌套数组

php - symfony2测试带有发布数据的客户请求