Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 2-提交表单的动态生成_Php_Symfony - Fatal编程技术网

Php Symfony 2-提交表单的动态生成

Php Symfony 2-提交表单的动态生成,php,symfony,Php,Symfony,我试图在symfony 2中实现一对多关系的Country->State->City。我刚开始使用symfony,我找到了一些关于这个的链接,但无法让它工作,因为这些都是旧版本的symfony 我已经生成了3个实体,如国家、州和城市 注:城市与国家没有直接关系,而是通过国家间接联系 我为所有这些实体生成了CRUD 州和国家工作的伟大,但我需要选择国家,然后国家和进入城市的城市创建形式 我下面的教程从symfony食谱从 我的城市类型如下所示 namespace Aceonics\SystemBu

我试图在symfony 2中实现一对多关系的Country->State->City。我刚开始使用symfony,我找到了一些关于这个的链接,但无法让它工作,因为这些都是旧版本的symfony

我已经生成了3个实体,如国家、州和城市

注:城市与国家没有直接关系,而是通过国家间接联系

我为所有这些实体生成了CRUD

州和国家工作的伟大,但我需要选择国家,然后国家和进入城市的城市创建形式

我下面的教程从symfony食谱从

我的城市类型如下所示

namespace Aceonics\SystemBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;

use Aceonics\SystemBundle\Entity\Country;
use Aceonics\SystemBundle\Entity\State;

class CityType extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('country', 'entity', array(
                'class'         => 'AceonicsSystemBundle:Country',
                'property'      => 'name',
                    //This line is added as City and Country do not have direct relation
                'mapped'        => false, 
                'empty_value'   => 'Choose an option'
            ))
            ->add('name')
        ;   

        $formModifier = function (FormInterface $form, Country $country = null) {
            $states = null === $country ? array() : $country->getStates();

            $form->add('state', 'entity', array(
                'class'       => 'AceonicsSystemBundle:State',
                'choices'     => $states,
                'empty_value' => 'Choose an option'
            ));
        };

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) use ($formModifier) {
                $data = $event->getData();

                //Error Occurs here.
                //As per tutorial it should be $data->getCountry()
                //But as I do not have direct relation I have used $data->getState()->getCountry
                $formModifier($event->getForm(), $data->getState()->getCountry());
            }
        );

        $builder->get('country')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) use ($formModifier) {
                $country = $event->getForm()->getData();
                $formModifier($event->getForm()->getParent(), $country);
            }
        ); 
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Aceonics\SystemBundle\Entity\City'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'aceonics_systembundle_city';
    }
}
如果使用
$data->getCountry()
,则会出现以下错误:

Attempted to call method "getCountry" on class "Aceonics\SystemBundle\Entity\City" in E:\localhost\Aceonics_Apps\PHP_Frameworks\symfony\src\Aceonics\SystemBundle\Form\CityType.php line 49
Error: Call to a member function getCountry() on a non-object in E:\localhost\Aceonics_Apps\PHP_Frameworks\symfony\src\Aceonics\SystemBundle\Form\CityType.php line 49
如果使用
$data->getState()->getCountry()
,则会出现以下错误:

Attempted to call method "getCountry" on class "Aceonics\SystemBundle\Entity\City" in E:\localhost\Aceonics_Apps\PHP_Frameworks\symfony\src\Aceonics\SystemBundle\Form\CityType.php line 49
Error: Call to a member function getCountry() on a non-object in E:\localhost\Aceonics_Apps\PHP_Frameworks\symfony\src\Aceonics\SystemBundle\Form\CityType.php line 49
我试着把这个放进if声明中,但没有用


请帮助我理解这一点并解决问题

跟随它是完美的,写得很好。谢谢@StivenLlupa让我读一下。这里是另一个非常好的教程-