在我的项目Symfony2中,我有一个与实体Projet
具有关系(1,n)的实体TypeConstruction
。
对象TypeConstruction
可以是对象Immeuble
或Villa
...
所以对象Immeuble
继承了对象TypeConstruction
...
实体TypeConstruction
的代码:
/**
* @ORM\Entity
* @ORM\Table(name="typeconstruction")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="typeFille", type="string")
* @ORM\DiscriminatorMap( {"typeConstruction" = "TypeConstruction", "lotissement" = "Lotissement", "immeuble" = "Immeuble", "appartement" = "Appartement", "villa" = "Villa", "piece" = "Piece"} )
*/
class TypeConstruction
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="numLot", type="string", length=255)
*/
private $numLot;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=1000)
*/
private $description;
/**
* @var float
*
* @ORM\Column(name="superficie", type="float")
*/
private $superficie;
/**
* @var float
*
* @ORM\Column(name="longueur", type="float")
*/
private $longueur;
/**
* @var float
*
* @ORM\Column(name="largeur", type="float")
*/
private $largeur;
/**
* @var float
*
* @ORM\Column(name="hauteur", type="float")
*/
private $hauteur;
/**
* @var float
*
* @ORM\Column(name="millieme", type="float")
*/
private $millieme;
/**
* @ORM\ManyToOne(targetEntity="Immo\IntervenantBundle\Entity\Proprietaire", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $proprietaires;
/**
* @ORM\ManyToOne(targetEntity="Immo\DossierBundle\Entity\Projet", inversedBy="typeConstructions")
* @ORM\JoinColumn(nullable=false)
*/
private $projet;
private $typeFille ;
//getter setter
.....}
实体Immeuble
的代码:
class Immeuble extends TypeConstruction
{
/**
* @var integer
*
* @ORM\Column(name="nbreBloc", type="integer")
*/
private $nbreBloc;
/**
* @var integer
*
* @ORM\Column(name="nbreEtage", type="integer")
*/
private $nbreEtage;
/**
* @ORM\ManyToOne(targetEntity="Immo\TypeConstructionBundle\Entity\Lotissement")
* @ORM\JoinColumn(nullable=true)
*/
private $lotissement;
/**
* @ORM\ManyToOne(targetEntity="Immo\TypeConstructionBundle\Entity\Type")
* @ORM\JoinColumn(nullable=false)
*/
private $type;
/**
* @ORM\OneToMany(targetEntity="Immo\TypeConstructionBundle\Entity\Bloc", mappedBy="immeuble")
*/
private $blocs;
...}
实体Projet
的代码:
class Projet
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="Code", type="string", length=255)
*/
private $code;
/**
* @var string
*
* @ORM\Column(name="Description", type="string", length=255)
*/
private $description;
/**
* @var \DateTime
*
* @ORM\Column(name="DateDebutPrev", type="datetime")
*/
private $dateDebutPrev;
/**
* @var \DateTime
*
* @ORM\Column(name="DateFinPrev", type="datetime")
*/
private $dateFinPrev;
/**
* @var \DateTime
*
* @ORM\Column(name="DateDebutReel", type="datetime",nullable=true)
*/
private $dateDebutReel;
/**
* @var \DateTime
*
* @ORM\Column(name="DateFinReel", type="datetime",nullable=true)
*/
private $dateFinReel;
/**
* @var integer
*
* @ORM\Column(name="NbreBatiment", type="integer")
*/
private $nbreBatiment;
/**
* @var integer
*
* @ORM\Column(name="NbreEtage", type="integer",nullable=true)
*/
private $nbreEtage;
/**
* @ORM\OneToMany(targetEntity="Immo\TypeConstructionBundle\Entity\TypeConstruction", cascade={"persist"}, mappedBy="projet")
*/
private $typeConstructions;
...}
在我的表单中,我在TypeConstructionType
中执行此操作:
class TypeConstructionType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('numLot')
->add('description')
->add('superficie')
->add('longueur')
->add('largeur')
->add('hauteur')
->add('millieme')
->add('typeFille', 'choice', array(
'choices' => array(
"lotissement" => "Lotissement",
"immeuble" => "Immeuble",
"appartement" => "Appartement",
"villa" => "Villa",
"piece" => "Piece"
),
'required' => false,
'empty_value' => 'Veuillez choisir',
'empty_data' => null
))
;
}...}
ImmeubleType
的代码:
class ImmeubleType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nbreBloc')
->add('nbreEtage')
->add('type', 'entity', array(
'class' => 'ImmoTypeConstructionBundle:Type',
'query_builder' => function(TypeRepository $repo) {
return $repo->getTypeOfTypeConstruction('immeuble');
},
'empty_value' => 'Veuillez choisir',
'property' => 'type',
'multiple' => false
))
->add('blocs', 'collection', array(
'type' => new BlocType(),
'prototype' => true,
'allow_add' => true,
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Immo\TypeConstructionBundle\Entity\Immeuble'
));
}
/**
* @return string
*/
public function getName()
{
return 'immo_typeconstructionbundle_immeuble';
}
public function getParent()
{
return new TypeConstructionType();
}
}
ProjetType
的代码:
class ProjetType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code','text')
->add('description','text')
->add('dateFinPrev','date')
->add('dateDebutPrev', 'date', array(
'widget' => 'single_text',
'input' => 'datetime',
'format' => 'dd/MM/yyyy',
))
->add('dateDebutReel','date')
->add('dateFinPrev', 'date', array(
'widget' => 'single_text',
'input' => 'datetime',
'format' => 'dd/MM/yyyy',
))
->add('dateFinReel','date')
->add('nbreBatiment')
->add('nbreEtage')
->add('typeConstructions', 'collection', array(
'type' => new TypeConstructionType(),
'allow_add' => true,
'allow_delete' => true
))
;
}...}
在添加项目的视图中,我有:
<div class="bloc" id="formTypeConstruction">
<div id="immo_dossierbundle_projet_typeConstructions" data-prototype="{% filter escape %}{% include 'ImmoTypeConstructionBundle:TypeConstruction:new.html.twig' with {'formTCAdd': form.typeConstructions.vars.prototype} %}{% endfilter %}">
</div>
</div><!--bloc-->
在ImmoTypeConstructionBundle:TypeConstruction:new.html.twig
中,我有代码显示typeConstruction的选择列表(Immeuble,Villa ....):
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="formulaire">
<div>
<label class="col-sm-12 col-md-2 col-lg-2 control-label" for="textinput">Type de construction</label>
<div class="col-sm-12 col-md-4 col-lg-4">
{{form_widget(formTCAdd.typeFille,{'attr':{'class': 'form-control selectpicker'}})}}
</div>
</div>
</div>
</div>
以添加Projet
的形式,我想从列表中选择TypeConstruction
,如果我选择类型{ImmeubleType
,则会显示Immeuble
的形式1}}。
我该怎么做?