我试图找出如何使用与Symfony的(2.8.6)继承类类型时处理形式。
我已经创造了什么,我想在下面做了[很]简单的例子情况。 有与它的问题,但它只是说明我的问题。
- 我怎样才能提供的一种形式从控制器将树枝模板,以便有可能选择什么“型”(鉴别)字段应该使用? 我应该简单地创建另一个变量,如在每个类的硬编码“型”?
- 一旦提交表单时,我怎么能弄清楚哪些类应该在控制器中使用,在任何一个“新”或“编辑”行为? 我已经试过拔除其拉出ParameterBag,创造适当的实体和形式,然后使用$形式 - >的handleRequest($请求); ...但它似乎并不时有可能属于另一种类型的额外下地干活。
如果有人甚至可以点我到Github上回购或东西,显示这样的事情发生,我会非常感激。 谢谢你的时间。
如果这些是我的课:
/**
* @ORM\Entity
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap("truck" = "Truck", "Car" = "Car", "suv" = "SUV")
*/
abstract class Vechicle {
private $make;
private $model;
private $numberOfDoors;
// getters and setters //
}
class Truck extends Vehicle {
private $offRoadPackage;
private $bedSize;
// getters and setters //
}
class Car extends Vehicle {
private $bodyType;
}
class SUV extends Vehicle {
// no additional fields //
}
然后像这将是我的表单类型:
class VehicleType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('make')
->add('model')
->add('numberOfDoors');
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\Vehicle'
));
}
}
class TruckType extends VehicleType {
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder
->add('offRoadPackage')
->add('bedSize');
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\Truck'
));
}
}
class CarType extends VehicleType {
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder
->add('bodyType')
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\Car'
));
}
}
class SUVType extends VehicleType {
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\SUV'
));
}
}