Php 类的Symfony2对象无法在最简单的代码上转换为字符串错误

Php 类的Symfony2对象无法在最简单的代码上转换为字符串错误,php,symfony,Php,Symfony,我有史以来最简单的代码,我不明白为什么我会得到这个错误。我只想创建一个表单来创建新的电子邮件。。。它看起来很简单,但就是不起作用 以下是错误: ContextErrorException: Catchable Fatal Error: Object of class MediaparkLt\UserBundle\Entity\Email could not be converted to string in C:\wamp\www\Digidis\front\app\cache\dev\twig

我有史以来最简单的代码,我不明白为什么我会得到这个错误。我只想创建一个表单来创建新的电子邮件。。。它看起来很简单,但就是不起作用

以下是错误:

ContextErrorException: Catchable Fatal Error: Object of class MediaparkLt\UserBundle\Entity\Email could not be converted to string in C:\wamp\www\Digidis\front\app\cache\dev\twig\e4\61\15c5455e50341edc1387cde083d07297f9c2cb3fe9cf4782bd2ed3ead531.php line 172
我试图清除我的缓存

这是实体:

<?php
/**
 * Created by PhpStorm.
 * User: domas
 * Date: 6/10/2015
 * Time: 2:18 PM
 */

namespace MediaparkLt\UserBundle\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Mp\CmsBundle\Entity\CmsElement;
use Mp\CmsBundle\Entity\CmsImage;
use Mp\CmsBundle\Entity\CmsImageGallery;
use MediaparkLt\LotteryBundle\Entity\Product;
use MediaparkLt\SkinBundle\Entity\UrlSkin;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * MediaparkLt\UserBundle\Entity\Email
 *
 * @ORM\Entity
 * @ORM\Table(name="email")
 */
class Email {

    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     */
    protected $id;

    /**
     * @var string $title
     *
     * @ORM\Column(name="title", type="string")
     */
    protected $title;

    /**
     * @var string $title
     *
     * @ORM\Column(name="content", type="string")
     */
    protected $content;

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

    public function setId() {
        $this->id = null;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Email
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

    /**
     * Set content
     *
     * @param string $content
     * @return Email
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

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

我发现两个可能的问题:

  • 正如@Gerry在评论中所建议的,您可能以以下方式在
    细枝中呈现实体:

    {{ email }}
    
    由于您的
    电子邮件
    实体没有
    \uuuu-toString
    ,因此它会失败

  • getName()
    返回
    email
    。我相信
    Symfony2
    已经为
    EmailType
    内部注册了该表单类型名称。因此,
    Symfony
    可能认为您试图呈现的是内部
    EmailType
    ,而不是您自己的

    尝试将名称更改为类似“我的电子邮件”

  • 希望这能有所帮助。

    为了便于调查,请尝试在实体类中实现
    \uu toString()
    方法。您似乎正在尝试在模板中呈现实体。
    {{ email }}