S
S
symnoob2019-06-23 23:01:33
symfony
symnoob, 2019-06-23 23:01:33

How to properly cure this error: could not be converted to string?

Hello everyone,
I generated ManyToMany and created CRUD, but for some reason it gives an error:

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

here are two entities:
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

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

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

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

    public function getBenutzer(): ?string
    {
        return $this->benutzer;
    }

    public function setBenutzer(string $benutzer): self
    {
        $this->benutzer = $benutzer;

        return $this;
    }


    public function __toString(){
        // to show the name of the Category in the select
        return $this->benutzer;
    }

}

<?php

namespace App\Entity;

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

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

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Account")
     */
    private $account;

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

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

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

    /**
     * @return Collection|Account[]
     */
    public function getAccount(): Collection
    {
        return $this->account;
    }

    public function addAccount(Account $account): self
    {
        if (!$this->account->contains($account)) {
            $this->account[] = $account;
        }

        return $this;
    }

    public function removeAccount(Account $account): self
    {
        if ($this->account->contains($account)) {
            $this->account->removeElement($account);
        }

        return $this;
    }

    public function getVorname(): ?string
    {
        return $this->vorname;
    }

    public function setVorname(string $vorname): self
    {
        $this->vorname = $vorname;

        return $this;
    }
}

even found a solution, but I think it is not correct:
If you insert this code into the Entity Account, then everything is fine. but this is a dumb approach, there must be another solution!
public function __toString(){
        return $this->benutzer;
    }

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2019-06-24
@Austin_Powers

Defining a method __toStringis a normal decision.
This error

Catchable Fatal Error: Object of class App\Entity\Account could not be converted to string
, wherever it is, and says that the object cannot be cast to a string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question