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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题