在第二个ChoiceType中选择一个gouvernorat后,在choiceType中加载城市

时间:2018-05-21 16:39:36

标签: forms symfony abstract-type

我有两个与Gouvernorat关联的实体 - > Villes。

这是Gouvernorat实体:

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

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100, nullable=false)
     */
    private $name;
    /**
     * @ORM\OneToMany(targetEntity="EntiteBundle\Entity\Ville", mappedBy="idGouvernorat")
     */
    private $villes;
    //getters & setters & constructor
}

这是城市实体:

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

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

/**
 * @var \Gouvernorat
 *
 * @ORM\ManyToOne(targetEntity="EntiteBundle\Entity\Gouvernorat",inversedBy="villes")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_gouvernorat", referencedColumnName="id")
 * })
 */
private $idGouvernorat;
}

在名为Etablissement的第三个实体中,我有字符串gouvernorat和字符串字符串ville。所以我试图创建一个EtablissementType。此表包含 两个实体类型为孩子gouvernorat和ville 简要说明:

class EtablissementType extends AbstractType
{  

 public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
        ->add('nom')           
        ->add('gouvernorat', EntityType::class, array(
            'class' => 'EntiteBundle\Entity\Gouvernorat',
            'choice_label' => 'name',
            'multiple' => false
        ))          
        ->add('ville',EntityType::class,
            array(
                'class' => 'EntiteBundle\Entity\Ville',
                'choice_label' => 'name',
                'multiple' => false
        ))            
        ->add('Enregistrer',SubmitType::class)
        -> setMethod('POST');       
}

当我选择gouvernorat时,我希望这个gouvernorat的所有城市都加载我第二个ChoiceType,所以我添加了这个事件:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
            //$builder-> ...

            $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) {
                $form = $event->getForm();

                $data = $event->getData();                    
                $gouv = $data-> getGouvernorat();
                $villes = null === $gouv ? array() : $gouv->getVilles();
                $names = array_map(function ($value) {
                    return  $value['name'];
                }, $villes);

                $form->add('ville', ChoiceType::class,
                    array(
                    'choices' => $names
                ));
            }
        );
    }

数据的快捷方式(以json格式):

[
{
    "id": 1,
    "name": "ARIANA",
    "villes": [
        {
            "id": 1,
            "name": "RAOUED",
            "idGouvernorat": null
        },
        {
            "id": 2,
            "name": "SIDI THABET",
            "idGouvernorat": null
        },
        {
            "id": 3,
            "name": "ARIANA VILLE",
            "idGouvernorat": null
        },
        {
            "id": 4,
            "name": "LA SOUKRA",
            "idGouvernorat": null
        },
        {
            "id": 5,
            "name": "KALAAT LANDLOUS",
            "idGouvernorat": null
        },
        {
            "id": 6,
            "name": "ETTADHAMEN",
            "idGouvernorat": null
        },
        {
            "id": 7,
            "name": "MNIHLA",
            "idGouvernorat": null
        }
    ]
},
{
    "id": 2,
    "name": "BEJA",
    "villes": [
        {
            "id": 8,
            "name": "BEJA NORD",
            "idGouvernorat": null
        },
        {
            "id": 9,
            "name": "NEFZA",
            "idGouvernorat": null
        },
        {
            "id": 10,
            "name": "GOUBELLAT",
            "idGouvernorat": null
        },
        {
            "id": 11,
            "name": "MEJEZ EL BAB",
            "idGouvernorat": null
        },
        {
            "id": 12,
            "name": "AMDOUN",
            "idGouvernorat": null
        },
        {
            "id": 13,
            "name": "TEBOURSOUK",
            "idGouvernorat": null
        },
        {
            "id": 14,
            "name": "TESTOUR",
            "idGouvernorat": null
        },
        {
            "id": 15,
            "name": "THIBAR",
            "idGouvernorat": null
        },
        {
            "id": 16,
            "name": "BEJA SUD",
            "idGouvernorat": null
        }
    ]
},

...等 问题是,第二个选项类型总是空的,表示$gouv->getVilles();不起作用 问题在哪里,我没有找到问题,我使用了symfony documentation--Dynamic Generation for Submitted Forms

<script>
                    var $gouvernorat = $('#etablissement_gouvernorat');
                    // When gouvernorat gets selected ...
                    $gouvernorat.change(function() {
                        // ... retrieve the corresponding form.
                        var $form = $(this).closest('form');
                        // Simulate form data, but only include the selected gouvernorat value.
                        var data = {};
                        data[$gouvernorat.attr('name')] = $gouvernorat.val();
                        // Submit data via AJAX to the form's action path.
                        $.ajax({
                            url : $form.attr('action'),
                            type: $form.attr('method'),
                            data : data,
                            success: function(html) {
                                // Replace current ville field ...
                                $('#etablissement_ville').replaceWith(
                                    // ... with the returned one from the AJAX response.
                                    $(html).find('#etablissement_ville')
                                );
                                // Ville field now displays the appropriate Villes.
                            }
                        });
                    });
                </script>

1 个答案:

答案 0 :(得分:1)

首先,当您在表单中获取gouv数据时,我不确定它是否按预期工作。

替换

$form = $event->getForm();
$data = $event->getData();                 
$gouv = $data->getGouvernorat();

人:

$form = $event->getForm();
$data = $event->getData();                
$gouv = $data->get('gouvernorat')->getData(); // basically, you get the form data for the gouvernorat field

此刻我认为gouvernorat没有自己的villes,这些villes存储在数据库中(我想)。 现在,您需要检查$ data(这是一个Etablissement实例)的 getGouvernorat 方法是否返回 Gouvernorat实例。 如果是,您将从您的数据库中获取此gouvernerat,以便访问其所有城市。

if (!is_null($data->getGouvernorat())) {
    $persistedGouvernorat = $gouvernoratRepository->find($event->getData()->getGouvernorat());;
    if (!is_null($persistedGouvernorat)) {
        $gouverorat = $persistedGouvernorat;
    }
}
$villes = $gouvernorat->getVilles();

然后我认为你可以摆脱你的问题。