duanmeng1950
2019-05-02 08:34
浏览 177
已采纳

Symfony 4 - 类的对象无法转换为String

After looking over the internet and here I didn' find something useful for me. I'm currently creating a CRUD. Every 'Entreprise' having one or multiple 'Site' and I'm currently doing the CRUD for Site. I've made it by doing the make:form command. Whhen I'm going to create a site the following error appear :

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

I've tried to add the function __toString() as i saw. But maybe i didn't add it crrectly it changes nothing so I removed it.

My controller to create a site looks like this :

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

My SiteType generate by the make:form commande :

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

So here are my ENTITY

Entreprise

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

I didn't know what's wrong. Maybe the __toString I didn't write correclty ! I've wrote :

public function __toString()
    {
        return $this->getSiteNom();
    }
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqian8238 2019-05-02 13:46
    已采纳

    Catchable Fatal Error: Object of class App\Entity\Entreprise

    You need to implement the __toString() method in the Entreprise entity

    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;
        }
    
        // ...
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题