我的表单返回以下错误:
执行' UPDATE user_info SET时发生异常 country_of_residence = ?, want_children = ?, salary =? WHERE id =?' 使用params [{}," a"," 0",22]:
捕获致命错误:类AppBundle \ Entity \ Country的对象可以 不能转换为字符串
我的UserInfoType:
<?php
// src/AppBundle/Form/UserInfoType.php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class UserInfoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('first_name', TextType::class, array('label' => 'Prénom'))
->add('surname', TextType::class, array('label' => 'Nom'))
->add('nationality', TextType::class, array('label' => 'Nationalité'))
->add('country_of_residence', EntityType::class, array(
'class' => 'AppBundle:Country',
'choice_label' => 'name',
))
->add('maritial_status', TextType::class, array('label' => 'Votre statut maritial'))
->add('ethnique_origin', TextType::class, array('label' => 'Origine ethinique'))
->add('spoken_language', TextType::class, array('label' => 'Langues parlés'))
->add('children', TextType::class, array('label' => 'Possèdez vous des enfants'))
->add('want_children', TextType::class, array('label' => 'Désirez vous des enfants'))
->add('astrological_sign', TextType::class, array('label' => 'Votre signe astrologique'))
->add('education', TextType::class, array('label' => 'Votre niveau d\'étude'))
->add('profession', TextType::class, array('label' => 'Votre profession'))
->add('salary', TextType::class, array('label' => 'Votre salaire'))
->add('save', SubmitType::class, array('label' => 'Suivant'))
;
}
我的国家实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Country
*
* @ORM\Table(name="country")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CountryRepository")
*/
class Country
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="code", type="string", length=255)
*/
private $code;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="language", type="string", length=255)
*/
private $language;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set code
*
* @param string $code
*
* @return Country
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set name
*
* @param string $name
*
* @return Country
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set language
*
* @param string $language
*
* @return Country
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* @return string
*/
public function getLanguage()
{
return $this->language;
}
}
我的profil控制器:
/**
* @Route("/survey/{slug}", name = "survey_render" )
*/
public function renderSurvey($slug, Request $request)
{
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$user_info = $em->getRepository('ProfileBundle:UserInfo')->findOneBy(array('userId' => $user->getId()));
if (!$user_info) {
$user_info = new UserInfo();
$user_info->setuserId($user->getId());
}
$user_pref = $em->getRepository('ProfileBundle:UserPreference')->findOneBy(array('userId' => $user->getId()));
if (!$user_pref) {
$user_pref = new UserPreference();
$user_pref->setuserId($user->getId());
}
//$user_info = new UserInfo();
switch ($slug) {
case 'vos_infos':
$form = $this->createForm(UserInfoType::class, $user_info );
break;
case 'votre_apparance':
$form = $this->createForm(UserAppearanceType::class, $user_info);
break;
case 'votre_style_vie':
$form = $this->createForm(UserLifeStyleType::class, $user_info);
break;
case 'ses_infos':
$form = $this->createForm(UserPrefInfoType::class, $user_pref);
break;
case 'son_apparence':
$form = $this->createForm(UserPrefAppearanceType::class, $user_pref);
break;
case 'son_style_vie':
$form = $this->createForm(UserPrefLifeStyleType::class, $user_pref);
break;
case 'votre_quiz':
$form = $this->createForm(UserLifeStyleType::class, $user_pref);
break;
default;
echo 'Lien incorrect';
break;
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$form_data = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($form_data);
$em->flush();
似乎它试图将整个实体传递给表单,而不仅仅是选择结果。
有没有人知道我在这里做错了什么?
THX
答案 0 :(得分:1)
好的,我发现了UserInfo中的错误。
我没有预先安装@ORM并且没有到Country实体的正确路径(它不在同一名称空间中)
之前:
/**
* One Product has One Shipping.
* @OneToOne(targetEntity="Country")
* @JoinColumn(name="countryOfResidence", referencedColumnName="id")
*/
private $countryOfResidence;
后:
/**
* One Product has One Shipping.
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Country")
* @ORM\JoinColumn(name="countryOfResidence", referencedColumnName="id")
*/
private $countryOfResidence;
感谢您指出我正确的方向。
答案 1 :(得分:0)
如果user_info是一个实体,您是否已将country_of_residence字段设置为国家/地区实体的onetoone关系?
答案 2 :(得分:0)
我遇到了同样的错误。就我而言,它是“拥有”实体中的错误注释。
Befor (导致错误):
/**
* @ORM\OneToOne(targetEntity="RfpMatchBundle\Entity\Address", cascade={"persist"})
* @ORM\Column(nullable=true, unique=false)
*/
private $address;
<强>后强>(固定):
/**
* @ORM\OneToOne(targetEntity="RfpMatchBundle\Entity\Address", cascade={"persist"})
*/
private $address;`