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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?