Symfony Catchable致命错误:类AppBundle \ Entity \ Email的对象无法转换为字符串

时间:2015-10-20 06:45:03

标签: php symfony doctrine-orm

我是symfony的新手,我创建了一个附加的小型数据库架构: enter image description here

电子邮件类:

    <?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Email
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\EmailRepository")
 */
class Email extends Service
{

    /**
     * @var string
     *
     * @ORM\Column(name="emailAddress", type="string", length=255)
     */
    private $emailAddress;




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

        return $this;
    }

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


}

和我的服务类:

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\ORM\Mapping as ORM;

/**
 * Service
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\ServiceRepository")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"newsletter" = "Newsletter", "email" = "Email", "service" = "Service"})
 * 
 */
class Service
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="serviceTitle", type="string", length=255)
     */
    private $serviceTitle;


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

    /**
     * Set serviceTitle
     *
     * @param string $serviceTitle
     *
     * @return Service
     */
    public function setServiceTitle($serviceTitle)
    {
        $this->serviceTitle = $serviceTitle;

        return $this;
    }

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


    /**
     * @ORM\ManyToOne(targetEntity="Service", inversedBy="children")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="Service", mappedBy="parent")
     */
    private $children;
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->children = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set parent
     *
     * @param \AppBundle\Entity\Service $parent
     *
     * @return Service
     */
    public function setParent(\AppBundle\Entity\Service $parent = null)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     *
     * @return \AppBundle\Entity\Service
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Add child
     *
     * @param \AppBundle\Entity\Service $child
     *
     * @return Service
     */
    public function addChild(\AppBundle\Entity\Service $child)
    {
        $this->children[] = $child;

        return $this;
    }

    /**
     * Remove child
     *
     * @param \AppBundle\Entity\Service $child
     */
    public function removeChild(\AppBundle\Entity\Service $child)
    {
        $this->children->removeElement($child);
    }

    /**
     * Get children
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChildren()
    {
        return $this->children;
    }




}

然后我为电子邮件生成crud,它工作正常,现在我实际上想要添加两个服务字段(SERVICE TITLE和PARENT SERVICE)

我做了什么,我只是打开了电子邮件类型表单并添加了两个字段(服务标题和父服务):

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class EmailType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('emailAddress')
            ->add('serviceTitle')
            ->add('parent')
            ;


    }

保存后,当我创建新的电子邮件服务浏览器返回异常时:

捕获致命错误:类AppBundle \ Entity \ Email的对象无法转换为字符串

现在我只是在文本框中添加父ID(输入类型文本),但实际上我想使用(选择lise)用户可以在创建新服务时设置父服务,并且该选项列表包含所有服务以前创建的服务

1 个答案:

答案 0 :(得分:1)

在您的电子邮件类中添加 __ toString()方法:

public function __toString()
{
    return $this->emailAddress;
}