lundi 2 mars 2015

Symfony2: create a dynamic form following entities which are not necessarily related

I need to create a dynamic form, following this example in the doc here.


But my case is a little more specific and different than in this odc.


I have 4 entites: Parcsimmobilier.php, Ensembles.php, Batiments.php and Zonestechnique.php.


A Parcsimmobilier can have many Ensembles (ManyToOne relation), an Ensembles can have many Batiments (ManyToOne), a Batiment can have many Zonestechnique (ManyToOne).


This is the code for Parcsimmobilier.php:



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

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

/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Ensembles", mappedBy="parcsimmobilier")
*/
private $ensembles;


The code for Ensembles.php:



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

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

/**
* @var \Parcsimmobilier
*
* @ORM\ManyToOne(targetEntity="Parcsimmobilier")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="parcsimmobilier_id", referencedColumnName="id")
* })
*/
private $parcsimmobilier;


The code for Batiments.php:



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

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

/**
* @var \Ensembles
*
* @ORM\ManyToOne(targetEntity="Ensembles")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="ensembles_id", referencedColumnName="id")
* })
*/
private $ensembles;


And finally the code for Zonestechnique.php:



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

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

/**
* @var \Batiments
*
* @ORM\ManyToOne(targetEntity="Batiments")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="batiments_id", referencedColumnName="id")
* })
*/
private $batiments;

/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Categorieszonestechnique")
* @ORM\JoinTable(name="zonestechnique_categorieszonestechnique",
* joinColumns={
* @ORM\JoinColumn(name="zonestechnique_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="categorieszonestechnique_id", referencedColumnName="id")
* }
* )
*/

private $categorieszonestechnique;


For now, all is fine. But the problem is I need to create a dynamic form for the creation of one Zonestechnique which have no relation with Ensembles.php or Parcsimmobilier.php in her class.


The dynamic form have to work like this: 1- When I proceed the route form, a dropdownlist allow user to choose a Parcsimmobilier 2- When a Parcsimmobilier is chosen, a dropdownlist for Ensembles belong to the parcsimmobilier selected appears. 3- When an Ensemble is selected, a choice list appears in order to allow users to select one or many categorieszonestechnique. 4- If the category "outside" is selected, a dropdownlist for Batiments appears, but like the category is "outside, the dropdownlist have to be "frozen" whith the value "no batiment" selected. But if the category is not "outside", a dropdownlist for Batiments appears with all Batiments belong to the ensemble selected before, belong to the parcsimmobilier selected too. 5- When the batiment is selected the rest of the form for Zonestechnique appears.


I know it means a lot, but in fact the dynamic form is to create a new Zonestechnique, and this entity have no relation with Parcsimmobilier.php or Ensembles.php. I'm not asking for the code who solved my answer directly.


I'm asking for suggestions in order to succeed because I don't really know how to really proceed for making this dynamic form. I know that I have to use ajax, but how can I use this in my case.


Someone knows how could it works?


Thank you in advance.


Aucun commentaire:

Enregistrer un commentaire