Symfony2 __toString () error

I had a problem saving objects for me:

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

It is strange that the __ toString () method is a Type entity!

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: if I put cascade = {"persist"} the MailTelCont class with respect to ManyToOne, the type "type" does not show me this error, but it saves the 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 the nested "AnagraficType" as follows:

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?

+5
source share
1 answer

I think then these values null. You tried:

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

and

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

That way, when the value nullis passed to the string, and you should not get this exception.

+13
source

All Articles