dongtaijiao7140 2015-06-10 12:43
浏览 54
已采纳

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

I have the most simplest code ever, and I cant understand why the hell do I get this error. I just want to create a form to create a new email message... It looks so simple, yet it just doesnt work....

Here is the error:

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

I tried clearing my cache.

This is the entity:

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

This is the form I want to create:

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

namespace MediaparkLt\SkinBundle\Form\Type;

use MediaparkLt\MainBundle\Form\Type\TranslatableType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\AbstractType;

class EmailType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $option) {
        $builder->add('title', 'text', array('label' => 'cms.Title'));

        $builder->add('content', 'text', array('label' => 'cms.Content'));
    }

    public function getDefaultOptions(array $options) {
        return array(
            'data_class' => 'MediaparkLt\UserBundle\Entity\Email',
        );
    }


    public function getName()
    {
        return 'email';
    }
}

Someone help!

  • 写回答

1条回答 默认 最新

  • doumu6997 2015-06-10 13:07
    关注

    I see two possible problems:

    1. As @Gerry suggested in the comment, you might be rendering the entity in your Twig template in a manner:

      {{ email }}
      

      and since your Email entity does not have __toString it fails.

    2. You getName() returns email. I believe Symfony2 has that form type name registered internally for EmailType. So, Symfony might be thinking you are trying to render internal EmailType instead of your own.

      Try changing the name to something like my_email.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?