Symfony2 __toString() error

2019-04-08 18:23发布

I have a problem with saving entities back to me this error:

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

The strange thing is that the method __ toString () is the entity Type!

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();
}
//...

Another strange thing is that if I put cascade ={"persist"} class MailTelCont on ManyToOne relationship "type" does not show me this error, but save a new field in Type ..

class 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();
}

Call the form of nested "AnagraficType" in this way:

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
            ))

Where am I doing wrong?

1条回答
欢心
2楼-- · 2019-04-08 18:56

I think those values are null then. Did you try:

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

and

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

That way when value is null will be casted to string and you should not get this exception.

查看更多
登录 后发表回答