简体   繁体   English

Symfony2:将一个formType嵌入另一个formType中,以引用addAction()方法的外键(多个form Type)

[英]Symfony2: imbricate a formType in another formType refer to the foreign key for addAction() method (multiple form Type)

In my project on Symfony, I need to include/imbricate a formType in another one , refer to the foreign key . 在我的Symfony的项目,我需要包括/在另一个覆瓦状排列一个formType,指的是外键 Indeed, when I proceed the addAction() method in my controller, I have to add my datas in the two tables at the same time, and it must match to the foreign key and the relation between my two tables. 确实,当我在控制器中执行addAction() 方法时,我必须同时将数据添加到两个表中,并且它必须与外键以及两个表之间的关系匹配。

This my first class/entity: 这是我的头等舱/实体:

class Zonestechnique
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=150, nullable=false)
     */
    private $nom;

    //getters and setters

This entity is linked with another where the foreign key is, look at my second entity/class: 这个实体与另一个外键是链接的,请看我的第二个实体/类:

class Conformiteszonestechnique
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="precision", type="text", nullable=true)
     */
    private $precision;
    /**
     * @var \Zonestechnique
     *
     * @ORM\ManyToOne(targetEntity="Zonestechnique")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="zonestechnique_id", referencedColumnName="id")
     * })
     */
    private $zonestechnique;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->zonestechnique = new \Doctrine\Common\Collections\ArrayCollection();
    }
    //getters and setters

As you can see, the $zonestechnique is the foreign key. 如您所见, $zonestechnique是外键。 These entities are linked by a ManyToOne relation. 这些实体通过ManyToOne关系链接。

I need to include the ZonestechniqueType form to the ConformiteszonestechniqueType. 我需要将ZonestechniqueType表单包括在ConformiteszonestechniqueType中。

look at my code: 看我的代码:

class ZonestechniqueType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom')
            ->add('localisation')
            ->add('commentaire')
            ->add('batiments')
            ->add('categorieszonestechnique')
        ;
    }

This form Builder interface have to be include in the ConformiteszonestechniqueType : 此Form Builder接口必须包含在ConformiteszonestechniqueType

class ConformiteszonestechniqueType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('precision')
            ->add('dateverification')
            ->add('typesetatsconformite')
            ->add('typesconformitezonestechnique')
            ->add('zonestechnique')
        ;
    }

So I need to include the ZonestechniqueType formBuilder to the other ConformiteszonestechniqueType for my addAction() method in my controller. 因此,我需要在我的控制器中为我的addAction()方法将ZonestechniqueType包含到其他ConformiteszonestechniqueType中。 So when I add datas in ConformiteszonestechniqueType , I have datas in ZonestechniqueType , in that way my relation between the two entities remains consistent with my database. 因此,当我在ConformiteszonestechniqueType中添加数据时,我在ZonestechniqueType中具有数据, 这样 ,两个实体之间的关系就与数据库保持一致。

This is my controller : 这是我的controller

public function addAction() {

        $em=$this->getDoctrine()->getManager();
        $zoneconformite = new Conformiteszonestechnique;
        $form = $this->createForm(new ConformiteszonestechniqueType(), $zoneconformite);
        $request = $this->getRequest();

        if ($request->isMethod('POST') | ($form->isValid()) ) {

                $form->bind($request);
                $zoneconformite = $form->getData();
                $em->persist($zoneconformite);
                $em->flush();

                return $this->redirect($this->generateUrl('myRoute'));

How Can I proceed? 我该如何进行? It is the right way? 这是正确的方法吗?

I think I have to use the collection field in the formType. 我想我必须在formType中使用collection字段。


UPDATE UPDATE

I change my ConformiteszonestechniqueType to this: 我将我的ConformiteszonestechniqueType更改为此:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('precision')
            ->add('dateverification')
            ->add('typesetatsconformite')
            ->add('typesconformitezonestechnique')
            ->add('zonestechnique', 'collection', array('type' =>new ZonestechniqueType() ) )
        ;
    }

I have no error occured, but when I display the form on my browser, I have not the form for zonestechnique . 我没有发生任何错误,但是当我在浏览器中显示表单时,我没有zonestechnique的表单。 In fact, the field for zonestechnique have a label named zonestechnique but there are no added fields. 实际上, zonestechnique的字段具有一个名为zonestechnique的标签,但是没有添加的字段。

Just have a look there: http://symfony.com/doc/current/cookbook/form/form_collections.html 在那里看看: http : //symfony.com/doc/current/cookbook/form/form_collections.html

The ZonestechniqueType is stored an an array, just loop through. ZonestechniqueType存储为一个数组,仅循环通过。

{% for zonestechnique in form.zonestechniques %}
    {{ form_row(zonestechnique.nom) }}
{% endfor %}

I have the same problem before. 我以前有同样的问题。 the fix that I come up to is by adding a default array with blank value in my entity's property. 我要解决的问题是在实体的属性中添加带有空白值的默认数组。 In your case its your "Conformiteszonestechnique". 您的情况是您的“ Conformiteszones技术”。 Try updating the $zonestechnique property with a default initialized value such as: 尝试使用默认的初始化值更新$ zonestechnique属性,例如:

/**
 * @var \Zonestechnique
 *
 * @ORM\ManyToOne(targetEntity="Zonestechnique")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="zonestechnique_id", referencedColumnName="id")
 * })
 */
private $zonestechnique = array(1); // or whatever id or name you want to initialize

Its up to you whether you initialize it in your constructor or inline but just make sure the datatype will match what you expect to. 由您决定是在构造函数中还是内联中对其进行初始化,但要确保数据类型符合您的期望。

The reason why you have this form field not appearing because form type "collection" is dynamic. 之所以没有此表单字段,是因为表单类型“集合”是动态的。 Meaning, you can add or remove field(s) from the form on both client and server side. 意思是,您可以在客户端和服务器端的表单中添加或删除字段。 It is not the same with other typical form types which are fixed to one form field item. 它与固定在一个表单字段项上的其他典型表单类型不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM