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

时间:2021-08-11 07:27:43

When i go to http://localhost:8000/stock/new i get the following error: Catchable Fatal Error: Object of class AppBundle\Entity\Location could not be converted to string

当我去http:// localhost:8000 / stock / new我得到以下错误:Catchable致命错误:类AppBundle \ Entity \ Location的对象无法转换为字符串

Location.php

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

I know i have to add __toSting somewhere, but it doens't work. Stock.php

我知道我必须在某处添加__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;
    }
}

1 个解决方案

#1


0  

Add method toString to Location entity:

将方法toString添加到Location实体:

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

then clear cache:

然后清除缓存:

bin/console cache:clear

The problem is that you are including Location entity to Stock form and symfony need to render somehow every location, so you need choose how it will be shown in form, toString could return any property that you want.

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

#1


0  

Add method toString to Location entity:

将方法toString添加到Location实体:

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

then clear cache:

然后清除缓存:

bin/console cache:clear

The problem is that you are including Location entity to Stock form and symfony need to render somehow every location, so you need choose how it will be shown in form, toString could return any property that you want.

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