使用实体自动构建表单(一对一关系)

时间:2021-10-05 06:48:37

I'm wondering if it is possible to build a form using an entity to get all the fields, in a one-to-one relationship. To clarify:

我想知道是否有可能使用实体构建一个表单以一对一的关系获取所有字段。澄清:

I have an User.php entity (with all the obvious fields, name, surname, genre, etc) and a Address.php entity. What I want is to build the whole form without adding one by one the properties of Address entity and save it with the proper relationship in the database.

我有一个User.php实体(包含所有明显的字段,名称,姓氏,类型等)和一个Address.php实体。我想要的是构建整个表单,而不是逐个添加Address实体的属性,并在数据库中保存适当的关系。

This is what I've tried (i've trimed the code a little bit), but obviously is not the correct way:

这就是我尝试过的(我对代码进行了一些尝试),但显然不是正确的方法:

User entity:

用户实体:

class User implements UserInterface {

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;


/**
 * @ORM\Column(type="string", length=100, nullable=TRUE)
 */
protected $firstName;

/**
 * @ORM\Column(type="string", length=200)
 * @Assert\NotBlank()
 */
protected $lastNames;

/**
 * @ORM\OneToOne(targetEntity="Capsa\Bundle\ClubCommonBundle\Entity\Address")
 */
protected $address;

Address class

地址类

class Address {

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=100, unique=TRUE)
 * @Assert\NotBlank()
 */
protected $streetName;

/**
 * @ORM\Column(type="string", length=50)
 */
protected $streetNumber;

Form builder:

表单构建器:

public function buildForm(FormBuilder $builder, array $options) {
    $builder->add('login', 'text')
            ->add('password', 'password')
            ->add('firstName', 'text', array("required" => FALSE))
            ->add('lastNames', 'text')
            ->add('address', 'entity', array(
                'class' => 'CapsaClubCommonBundle:Address',
                'property'=>'streetName'
            ));
}

That only gets the field streetName of the table and puts it in a list.

它只获取表的字段streetName并将其放入列表中。

1 个解决方案

#1


0  

Try to use Collection form type instead of Entity field type – see this tutorial.

尝试使用Collection表单类型而不是Entity字段类型 - 请参阅本教程。

#1


0  

Try to use Collection form type instead of Entity field type – see this tutorial.

尝试使用Collection表单类型而不是Entity字段类型 - 请参阅本教程。