Symfony -通过表单保存OneToOne关系时出错


Symfony - Error when saving OneToOne relationship via form

尝试保存包含单向OneToOne关系的提交表单时出现以下错误:

执行INSERT INTO person时发生异常(first_name, last_name, title, gender, phone1, phone2, mobile, fax,电子邮件,出生,add_info, address_id)值(?, ?, ?, ?, ?, ?, ?, ??, ?, ?)' 与params["自卫队","自卫队",空,"m",空,空,空,Null, Null, Null, Null, 12, "employee"]:

SQLSTATE[42P18]: Indeterminate datatype: 7 ERROR: could not determined .日志含义参数$13的数据类型

Edit:填写表单时不插入值"employee"。我不知道它是从哪里来的。

地址的ID似乎正确地自动生成,并且每次尝试保存表单时都会增加。

这是我的代码

:

/**
 * Person Model - used as parent for Employee and ContactPerson and instance in Customer
 * @ORM'Entity
 * @ORM'Table(name="person")
 * @ORM'Entity(repositoryClass="KuMiV'StoreBundle'Entity'PersonRepository")
 */
class Person 
{
  ... (all the other attributes)
   /**
    * @ORM'OneToOne(targetEntity="Address")
    * @ORM'JoinColumn(name="adress_id", referencedColumnName="id", nullable=false)
    */
   protected $address;
   ... (getter and setter)
}
员工:

名称空间KuMiV ' EmployeeBundle '实体;

使用KuMiV ' StoreBundle '实体'人;

使用Doctrine'ORM'Mapping作为ORM;use Symfony'Component'Validator'Constraints as Assert;

/**
 * Employee Model - extends from Person
 * @ORM'Entity
 * @ORM'InheritanceType("JOINED")
 * @ORM'DiscriminatorColumn(name="discr", type="string")
 * @ORM'DiscriminatorMap({"person" = "KuMiV'StoreBundle'Entity'Person", "employee" = "Employee"})
 * @ORM'Table(name="employee")
 * @ORM'Entity(repositoryClass="KuMiV'EmployeeBundle'Entity'EmployeeRepository")
 */
class Employee extends Person 
{   ...
   /**
   * @ORM'Column(type="string", length=50, nullable=true)
   * @Assert'Type(type="string")
   */
    private $svn 
    ... (getter and setter)
}

AddressForm:

class AddressForm extends AbstractType 
{
    // set the data class of the form
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'KuMiV'StoreBundle'Entity'Address',
        ));
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder       
            ->add('street', 'text', array(
                'label' => 'Straße*'))
            ->add('number', 'text', array(
                'label' => 'Hausnummer*'))
           ...
    }
    public function getName() 
    {
        return 'address';
    }
}

EmployeeForm:

class EmployeeForm extends AbstractType
{
    // set the data class of the form
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'KuMiV'EmployeeBundle'Entity'Employee',
            'cascade_validation' => true,
        ));
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', 'text', array(
                'label' => 'Titel', 'required' => false))
            ->add('firstName', 'text', array(
                'label' => 'Vorname*'))
            ->add('lastName', 'text', array(
            ...
            ->add('address', new AddressForm())   
            ...
            ->add('save', 'submit', array(
                'label' => 'Speichern', 'attr' => array('class' => 'btn btn-success')))
            ->getForm();
    }
    public function getName() 
    {
        return 'employee';
    }
}

控制器:

class DefaultController extends Controller
{
    public function indexAction()
    {
        return $this->render('EmployeeBundle:Default:index.html.twig');
    }
    public function newItemAction(Request $request)
    {
        $employee   = new Employee();
        $form       = $this->createForm(new EmployeeForm(), $employee);
        $form->handleRequest($request);    
        if ($form->isValid()) 
        {
            // save if submitted
            if ($form->isSubmitted()) 
            {
                $doctrineManager = $this->getDoctrine()->getManager();
                $doctrineManager->persist($employee);
                $doctrineManager->flush();
            }
        }
        return $this->render('EmployeeBundle:Form:newItem.html.twig', array(
                'form' => $form->createView(),
            ));
    }
}

编辑:我认为这与@ORM'DiscriminatorMap({"person" = "KuMiV'StoreBundle'Entity'Person", "employee" = "Employee"})中的"employee" = "Employee"有关因为这是我没有在我的表单中定义的doctrine insert末尾的字符串(在提交表单后也没有在$employee中定义)。

正如我在Person类中看到的,地址字段被引用到" address ",由于它的名称是address,因此不存在。

是不是你的代码也打错了?

在努力解决这个错误之后,我终于找到了解决方案。这很简单。我在错误的类中定义了教义元数据。

就像在原则参考中所说的:"@InheritanceType, @DiscriminatorColumn和@DiscriminatorMap必须指定在映射实体层次结构的最顶层类上。"

(在继承类中定义)