我在symfony2应用程序中有表单,但是当我发送表单时,我收到此消息:
Catchable Fatal Error: Object of class Lingogo\AdminBundle\Entity\Language could not be converted to string in C:\wamp\www\Lingogo\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php line 205
我检查我的实体很多时间,所有似乎都是正确的
实体/ Word.php
<?php
//src/Lingogo/AdminBundle/Entity/Word.php
namespace Lingogo\AdminBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Word
* @package Lingogo\AdminBundle\Entity
*
* @ORM\Entity
* @ORM\Table(name="words")
*/
class Word
{
/**
* @ORM\id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=80)
*/
protected $name;
/**
* @ORM\ManyToOne(targetEntity="Language", inversedBy="words")
* @ORM\JoinColumn(name="language", referencedColumnName="id")
*/
protected $language;
/**
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @ORM\Column(type="datetime")
*/
protected $updated;
/**
* @ORM\OneToMany(targetEntity="Translation", mappedBy="word")
*/
protected $translations;
/**
* @ORM\OneToMany(targetEntity="Definition", mappedBy="word")
*/
protected $definitions;
public function __construct()
{
$this->definitions = new ArrayCollection();
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Word
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set created
*
* @param \DateTime $created
* @return Word
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param \DateTime $updated
* @return Word
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* @return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Add translations
*
* @param \Lingogo\AdminBundle\Entity\Translation $translations
* @return Word
*/
public function addTranslation(\Lingogo\AdminBundle\Entity\Translation $translations)
{
$this->translations[] = $translations;
return $this;
}
/**
* Remove translations
*
* @param \Lingogo\AdminBundle\Entity\Translation $translations
*/
public function removeTranslation(\Lingogo\AdminBundle\Entity\Translation $translations)
{
$this->translations->removeElement($translations);
}
/**
* Get translations
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTranslations()
{
return $this->translations;
}
/**
* Add definitions
*
* @param \Lingogo\AdminBundle\Entity\Definition $definitions
* @return Word
*/
public function addDefinition(\Lingogo\AdminBundle\Entity\Definition $definitions)
{
$this->definitions[] = $definitions;
return $this;
}
/**
* Remove definitions
*
* @param \Lingogo\AdminBundle\Entity\Definition $definitions
*/
public function removeDefinition(\Lingogo\AdminBundle\Entity\Definition $definitions)
{
$this->definitions->removeElement($definitions);
}
/**
* Get definitions
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getDefinitions()
{
return $this->definitions;
}
/**
* Set language
*
* @param \Lingogo\AdminBundle\Entity\Language $language
* @return Word
*/
public function setLanguage(\Lingogo\AdminBundle\Entity\Language $language = null)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* @return \Lingogo\AdminBundle\Entity\Language
*/
public function getLanguage()
{
return $this->language;
}
}
这是我的第二个实体:Entity \ Language.php
<?php
//src/Lingogo/AdminBundle/Entity/Language.php
namespace Lingogo\AdminBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Language
* @package Lingogo\AdminBundle\Entity
*
* @ORM\Entity
* @ORM\Table(name="languages")
*/
class Language
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", unique=true, length=90)
*/
protected $language;
/**
* @ORM\Column(type="string", unique=true, length=6)
*/
protected $locale;
/**
* @ORM\OneToMany(targetEntity="Translation", mappedBy="languageTranslation")
*/
protected $translations;
/**
* @ORM\OneToMany(targetEntity="\Lingogo\PortalBundle\Entity\Course", mappedBy="nativeLanguage")
*/
protected $nativeLanguage;
/**
* @ORM\OneToMany(targetEntity="\Lingogo\PortalBundle\Entity\Course", mappedBy="courseLanguage")
*/
protected $courseLanguage;
/**
* @ORM\OneToMany(targetEntity="Definition", mappedBy="language")
*/
protected $definitions;
/**
* @ORM\OneToMany(targetEntity="Word", mappedBy="language")
*/
protected $words;
public function __construct()
{
$this->translations = new ArrayCollection();
$this->nativeLanguage = new ArrayCollection();
$this->courseLanguage = new ArrayCollection();
$this->definitions = new ArrayCollection();
$this->words = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set language
*
* @param string $language
* @return Language
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* @return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* Set locale
*
* @param string $locale
* @return Language
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* Get locale
*
* @return string
*/
public function getLocale()
{
return $this->locale;
}
/**
* Add translations
*
* @param \Lingogo\AdminBundle\Entity\Translation $translations
* @return Language
*/
public function addTranslation(\Lingogo\AdminBundle\Entity\Translation $translations)
{
$this->translations[] = $translations;
return $this;
}
/**
* Remove translations
*
* @param \Lingogo\AdminBundle\Entity\Translation $translations
*/
public function removeTranslation(\Lingogo\AdminBundle\Entity\Translation $translations)
{
$this->translations->removeElement($translations);
}
/**
* Get translations
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTranslations()
{
return $this->translations;
}
/**
* Add nativeLanguage
*
* @param \Lingogo\PortalBundle\Entity\Course $nativeLanguage
* @return Language
*/
public function addNativeLanguage(\Lingogo\PortalBundle\Entity\Course $nativeLanguage)
{
$this->nativeLanguage[] = $nativeLanguage;
return $this;
}
/**
* Remove nativeLanguage
*
* @param \Lingogo\PortalBundle\Entity\Course $nativeLanguage
*/
public function removeNativeLanguage(\Lingogo\PortalBundle\Entity\Course $nativeLanguage)
{
$this->nativeLanguage->removeElement($nativeLanguage);
}
/**
* Get nativeLanguage
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getNativeLanguage()
{
return $this->nativeLanguage;
}
/**
* Add courseLanguage
*
* @param \Lingogo\PortalBundle\Entity\Course $courseLanguage
* @return Language
*/
public function addCourseLanguage(\Lingogo\PortalBundle\Entity\Course $courseLanguage)
{
$this->courseLanguage[] = $courseLanguage;
return $this;
}
/**
* Remove courseLanguage
*
* @param \Lingogo\PortalBundle\Entity\Course $courseLanguage
*/
public function removeCourseLanguage(\Lingogo\PortalBundle\Entity\Course $courseLanguage)
{
$this->courseLanguage->removeElement($courseLanguage);
}
/**
* Get courseLanguage
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCourseLanguage()
{
return $this->courseLanguage;
}
/**
* Add definitions
*
* @param \Lingogo\AdminBundle\Entity\Definition $definitions
* @return Language
*/
public function addDefinition(\Lingogo\AdminBundle\Entity\Definition $definitions)
{
$this->definitions[] = $definitions;
return $this;
}
/**
* Remove definitions
*
* @param \Lingogo\AdminBundle\Entity\Definition $definitions
*/
public function removeDefinition(\Lingogo\AdminBundle\Entity\Definition $definitions)
{
$this->definitions->removeElement($definitions);
}
/**
* Get definitions
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getDefinitions()
{
return $this->definitions;
}
/**
* Add words
*
* @param \Lingogo\AdminBundle\Entity\Word $words
* @return Language
*/
public function addWord(\Lingogo\AdminBundle\Entity\Word $words)
{
$this->words[] = $words;
return $this;
}
/**
* Remove words
*
* @param \Lingogo\AdminBundle\Entity\Word $words
*/
public function removeWord(\Lingogo\AdminBundle\Entity\Word $words)
{
$this->words->removeElement($words);
}
/**
* Get words
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getWords()
{
return $this->words;
}
}
和我的Form \ Type \ WordType.php
<?php
//src/Lingogo/AdminBundle/Form/WordType.php
namespace Lingogo\AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class WordType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
$builder->add('language', 'entity', array(
'class' => 'LingogoAdminBundle:Language',
'property' => 'language',
'empty_value' =>'Please select language'
));
$builder->add('add', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class'=> 'Lingogo\AdminBundle\Entity\Word'
));
}
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'word';
}
}
我做错了什么?我该怎样做才能保存我的表格?
我发现了问题,我进行了验证
Lingogo\AdminBundle\Entity\Word:
properties:
name:
- NotBlank: { message: "word.name.not_blank" }
language:
- Choice: { choices: [pl, en], message: "word.language.choice"}
答案 0 :(得分:2)
将__toString()
方法添加到Language
实体类 - http://php.net/manual/en/language.oop5.magic.php#object.tostring