Symfony2 __toString() error
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.
Lughino
Always passionate about of computing and solving logic problems. In recent years I have gained experience in leadership in medium sized team. From the development side, I discovered a love for the MEAN stack, where the coupled javascript server side and NoSQL technologies have been adopted in all my projects.
Updated on October 31, 2020Comments
-
Lughino over 3 years
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?
-
Lughino about 11 yearsThank you for the reply, but now return this error:
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"}' however, save a new field in the Type, you do not must because the Type is only a container of choices of types of contact(eg Email, Fax, Phone..)