Catchable Fatal Error: Object of class AppBundle\Entity\Location could not be converted to string

Catchable Fatal Error: Object of class AppBundle\Entity\Location could not be converted to string

当我转到 http://localhost:8000/stock/new 时,出现以下错误:可捕获的致命错误:class AppBundle\Entity\Location 的对象无法转换为字符串

Location.php

namespace AppBundle\Entity;

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

/**
 * Location
 *
 * @ORM\Table(name="location")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\LocationRepository")
 */
class Location
{
    /**
     * @ORM\OneToMany(targetEntity="Stock", mappedBy="location")
     */
    private $stocks;

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

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="Code", type="string", length=10, unique=true)
     */
    private $code;

    /**
     * @var string
     *
     * @ORM\Column(name="Naam", type="string", length=255)
     */
    private $naam;

    /**
     * @var string
     *
     * @ORM\Column(name="Straatnaam", type="string", length=255)
     */
    private $straatnaam;

    /**
     * @var string
     *
     * @ORM\Column(name="Plaats", type="string", length=255)
     */
    private $plaats;

    /**
     * @var string
     *
     * @ORM\Column(name="Huisnummer", type="string", length=255)
     */
    private $huisnummer;

    /**
     * @var string
     *
     * @ORM\Column(name="Postcode", type="string", length=255)
     */
    private $postcode;

    /**
     * @var string
     *
     * @ORM\Column(name="Telefoonnummer", type="string", length=255)
     */
    private $telefoonnummer;

    /**
     * @var string
     *
     * @ORM\Column(name="Emailadres", type="string", length=255, unique=true)
     */
    private $emailadres;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set code
     *
     * @param string $code
     *
     * @return Location
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

    /**
     * Get code
     *
     * @return string
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * Set naam
     *
     * @param string $naam
     *
     * @return Location
     */
    public function setNaam($naam)
    {
        $this->naam = $naam;

        return $this;
    }

    /**
     * Get naam
     *
     * @return string
     */
    public function getNaam()
    {
        return $this->naam;
    }

    /**
     * Set straatnaam
     *
     * @param string $straatnaam
     *
     * @return Location
     */
    public function setStraatnaam($straatnaam)
    {
        $this->straatnaam = $straatnaam;

        return $this;
    }

    /**
     * Get straatnaam
     *
     * @return string
     */
    public function getStraatnaam()
    {
        return $this->straatnaam;
    }

    /**
     * Set plaats
     *
     * @param string $plaats
     *
     * @return Location
     */
    public function setPlaats($plaats)
    {
        $this->plaats = $plaats;

        return $this;
    }

    /**
     * Get plaats
     *
     * @return string
     */
    public function getPlaats()
    {
        return $this->plaats;
    }

    /**
     * Set huisnummer
     *
     * @param string $huisnummer
     *
     * @return Location
     */
    public function setHuisnummer($huisnummer)
    {
        $this->huisnummer = $huisnummer;

        return $this;
    }

    /**
     * Get huisnummer
     *
     * @return string
     */
    public function getHuisnummer()
    {
        return $this->huisnummer;
    }

    /**
     * Set postcode
     *
     * @param string $postcode
     *
     * @return Location
     */
    public function setPostcode($postcode)
    {
        $this->postcode = $postcode;

        return $this;
    }

    /**
     * Get postcode
     *
     * @return string
     */
    public function getPostcode()
    {
        return $this->postcode;
    }

    /**
     * Set telefoonnummer
     *
     * @param string $telefoonnummer
     *
     * @return Location
     */
    public function setTelefoonnummer($telefoonnummer)
    {
        $this->telefoonnummer = $telefoonnummer;

        return $this;
    }

    /**
     * Get telefoonnummer
     *
     * @return string
     */
    public function getTelefoonnummer()
    {
        return $this->telefoonnummer;
    }

    /**
     * Set emailadres
     *
     * @param string $emailadres
     *
     * @return Location
     */
    public function setEmailadres($emailadres)
    {
        $this->emailadres = $emailadres;

        return $this;
    }

    /**
     * Get emailadres
     *
     * @return string
     */
    public function getEmailadres()
    {
        return $this->emailadres;
    }

    /**
     * Add stock
     *
     * @param \AppBundle\Entity\Stock $stock
     *
     * @return Location
     */
    public function addStock(\AppBundle\Entity\Stock $stock)
    {
        $this->stocks[] = $stock;

        return $this;
    }

    /**
     * Remove stock
     *
     * @param \AppBundle\Entity\Stock $stock
     */
    public function removeStock(\AppBundle\Entity\Stock $stock)
    {
        $this->stocks->removeElement($stock);
    }

    /**
     * Get stocks
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getStocks()
    {
        return $this->stocks;
    }
}

我知道我必须在某处添加 __toSting,但它不起作用。 Stock.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Stock
 *
 * @ORM\Table(name="stock")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\StockRepository")
 */
class Stock
{
    /**
     * @ORM\ManyToOne(targetEntity="Location", inversedBy="stocks")
     * @ORM\JoinColumn(name="location_id", referencedColumnName="id")
     */
    private $location;
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     *
     * @ORM\Column(name="Aantal", type="integer")
     */
    private $aantal;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set aantal
     *
     * @param integer $aantal
     *
     * @return Stock
     */
    public function setAantal($aantal)
    {
        $this->aantal = $aantal;

        return $this;
    }

    /**
     * Get aantal
     *
     * @return int
     */
    public function getAantal()
    {
        return $this->aantal;
    }

    /**
     * Set location
     *
     * @param \AppBundle\Entity\Location $location
     *
     * @return Stock
     */
    public function setLocation(\AppBundle\Entity\Location $location = null)
    {
        $this->location = $location;

        return $this;
    }

    /**
     * Get location
     *
     * @return \AppBundle\Entity\Location
     */
    public function getLocation()
    {
        return $this->location;
    }
}

将方法 toString 添加到 Location 实体:

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

然后清除缓存:

bin/console cache:clear

问题是你将 Location 实体包含到 Stock 表单中,而 symfony 需要以某种方式呈现每个位置,所以你需要选择它在表单中的显示方式,toString 可以 return any 属性你想要的。