2013-02-17 8 views
5

मैं संस्थाओं मेरे पास वापस इस त्रुटि की बचत के साथ एक समस्या है:Symfony2 __toString() त्रुटि

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

एक और अजीब बात यह है कि अगर मैं ManyToOne संबंध "प्रकार" पर झरना = { "जारी रहती है"} वर्ग MailTelCont डाल मुझे इस त्रुटि नहीं दिखा, लेकिन प्रकार में एक नए क्षेत्र को बचाने करता है .. है

वर्ग 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 
      )) 

मैं गलत कहां कर रहा हूँ?

उत्तर

13

मुझे लगता है कि वे मान null हैं। आप की कोशिश की:

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

और

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

इस तरह किया जाता है जब मूल्य null स्ट्रिंग के लिए casted किया जाएगा और आप इस अपवाद नहीं मिलना चाहिए।

+0

जबाब, के लिए धन्यवाद, लेकिन अब यह त्रुटि लौटाते: 'एक नई इकाई संबंध 'मेरा \ BusinessBundle \ इकाई \ MailTelCont # प्रकार' कि झरना करने के लिए कॉन्फ़िगर नहीं किया गया था के माध्यम से मिला था इकाई के लिए संचालन जारी रहती है नाम: टेलीफ़ोन। इस समस्या को हल करने के लिए: या तो अज्ञात इकाई पर EntityManager # persist() को स्पष्ट रूप से कॉल करें या कॉन्फ़ेस कॉन्फ़िगर करें उदाहरण के लिए मैपिंग में इस एसोसिएशन को जारी रखें उदाहरण के लिए @ManyToOne (.., cascade = {"persist"}) ' अगर मैं 'कैस्केड = {"persist"} 'हालांकि, टाइप में एक नया फ़ील्ड सहेजें, आपको यह नहीं करना चाहिए क्योंकि टाइप केवल संपर्क प्रकारों (जैसे ईमेल, फैक्स, फोन ..) के विकल्पों का एक कंटेनर है। – Lughino

+0

यह मेरे लिए काम करता है, धन्यवाद ;) –