我有 3 个实体(国家、地区、城市)
namespace ****\****Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
class Country
{
private $id;
private $name;
/**
* @var integer $regions
*
* @ORM\OneToMany(targetEntity="Region", mappedBy="Country")
*/
protected $regions;
//...
}
class Region
{
private $id;
private $name;
/**
* @var integer $country
*
* @Assert\Type(type="****\***Bundle\Entity\Country")
* @ORM\ManyToOne(targetEntity="Country", inversedBy="regions")
* @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
*/
private $country;
/**
* @ORM\OneToMany(targetEntity="City", mappedBy="Region")
*/
protected $cities;
}
class City
{
private $id;
private $name;
/**
* @var integer $region
*
* @Assert\Type(type="****\****Bundle\Entity\Region")
* @ORM\ManyToOne(targetEntity="Region", inversedBy="cities")
* @ORM\JoinColumn(name="region_id", referencedColumnName="id", nullable=false)
*/
private $region;
/**
* @ORM\OneToMany(targetEntity="Company", mappedBy="City")
*/
protected $companys;
//...
}
这是我的城市表格类:
namespace ****\****Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('region');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '****\****Bundle\Entity\City',
));
}
public function getName()
{
return 'city';
}
}
这将创建一个基本的 HTML5 表单,其中包含名称的文本框和所有区域都可用的选择框。
我的问题是添加第一个 SelectBox 的最佳方法是什么,这将允许我选择国家以过滤第二个 SelectBox 并减少区域选择的数量?
事件监听器?事件调度程序组件 ?