【问题标题】:Symfony2 __toString() errorSymfony2 __toString() 错误
【发布时间】:2013-02-17 16:35:49
【问题描述】:

我在将实体保存回给我时遇到了这个错误:

Catchable Fatal Error: Method My\BusinessBundle\Entity\Type::__toString() 
must return a string value in
/var/www/MyBusiness0_1/vendor/doctrine/orm/lib/Doctrine/ORM/ORMInvalidArgumentException.php line 113

奇怪的是方法__toString()是实体类型!

class Type
{
 //..

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

/**
 * @ORM\OneToMany(targetEntity="MailTelCont", mappedBy="type")
 */
protected $mailTelContacts;

public function __construct()
{
    $this->mailTelContacts = new \Doctrine\Common\Collections\ArrayCollection();
}

public function __toString()
{
    return $this->getType();
}
//...

另一个奇怪的事情是,如果我将 cascade ={"persist"} class MailTelCont 放在 ManyToOne 关系 "type" 上不会显示此错误,而是在 Type 中保存一个新字段..

类 MailTelCont

class MailTelCont
{
 //..
/**
 * @var string
 *
 * @ORM\Column(name="contact", type="string", length=100)
 */
private $contact;

/**
 * @ORM\ManyToOne(targetEntity="Type", inversedBy="mailTelContacts")
 * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
 */
private $type;

/**
 * @ORM\ManyToOne(targetEntity="Anagrafica", inversedBy="mailTelContacts", cascade={"persist"})
 * @ORM\JoinColumn(name="anagrafica_id", referencedColumnName="id")
 * @Assert\Type(type="My\BusinessBundle\Entity\Anagrafica")
 */
private $anagrafica;

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

这样调用嵌套的“AnagraficType”形式:

class TypeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('type', 'entity', array(
                'class' => 'My\BusinessBundle\Entity\Type',
                'attr' => array('class' => 'conct'),
                'property' => 'type',
                'label' => 'Tipologia',
        ))
    ;
}
*****
class MailTelContType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('type', new TypeType())
        ->add('contact', 'text', array('label' => 'Contatto'))                
    ;
}
*****
class AnagraficaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('mailTelContacts', 'collection', array('type' => new MailTelContType(), 
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
                'by_reference' => false
            ))

我哪里做错了?

【问题讨论】:

    标签: symfony doctrine-orm symfony-2.1


    【解决方案1】:

    我认为这些值是null。你试过了吗:

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

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

    这样,当值为null 时,将被转换为字符串,您不应该得到这个异常。

    【讨论】:

    • 感谢您的回复,但现在返回此错误:A new entity was found through the relationship 'My\BusinessBundle\Entity\MailTelCont#type' that was not configured to cascade persist operations for entity: Telefono. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}) if I put 'cascade = {"persist"}' 但是,在类型中保存一个新字段,您不必因为Type 只是联系人类型选择的容器(例如电子邮件、传真、电话……)
    猜你喜欢
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2021-08-03
    • 2015-07-03
    • 2019-05-12
    • 2016-02-18
    • 1970-01-01
    相关资源
    最近更新 更多