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.

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

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。