Hello

I'm working on a doctrine/symfony2 project which needs to do the following:
Users can create or edit a "case"-entity in a form which contains
dropdowns, checkboxes, radio buttons etc. All fields and their
corresponding options should be loaded from the database and persisted
on the entity. The goal is to do this in a way, so I can just add a
new field in the database and it gets automatically added to the form
with all its options. What I currently did was the following:

//Entity/Case.php
/**
* Case
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Acme\MyBundle\Entity\CaseRepository")
*/
class Case
{
   /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
   private $id;

   /**
    *
    * @ORM\ManyToMany(targetEntity="Field", inversedBy="cases",
fetch="EAGER")
    * @ORM\JoinTable(name="case_fields",
    *          joinColumns={@ORM\JoinColumn(name="case_id",
referencedColumnName="id")},
    *      inverseJoinColumns={@ORM\JoinColumn(name="field_name",
referencedColumnName="name")}
    *      )
    */
   private $fields;

// getter and setter methods...
}


//Entity/Field.php
/**
* Field
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Acme\MyBundle\Entity\FieldRepository")
*/
class Field
{
   /**
    * @var integer
    * @ORM\Column(name="id", type="integer")
    */
   private $id;

   /**
    * @var string
    *
    * @ORM\Column(name="name", type="string", length=255, unique=true)
    * @ORM\Id
    */
   private $name;

   /**
    * @var string
    *
    * @ORM\Column(name="label", type="string", length=255)
    */
   private $label;

   /**
    * @var string
    * defines the type (e.g. dropdown, checkboxes, radiobuttons,
Y/N-radiobuttons etc)
    * @ORM\Column(name="type", type="string", length=1)
    */
   private $type;

   /**
    * @ORM\OneToMany(targetEntity="Option", mappedBy="field",
fetch="EAGER")
    *
    */
   private $options;

   /**
    * @ORM\ManyToMany(targetEntity="Case", mappedBy="fields",
fetch="EAGER", cascade="persist")
    */
   private $cases;


// getter and setter methods...
}


//Entity/Option.php
/**
* Option
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Acme\MyBundle\Entity\OptionRepository")
*/
class Option
{
   /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
   private $id;

   /**
    * @var integer
    *
    * @ORM\Column(name="value", type="integer")
    */
   private $value;

   /**
    * @var string
    *
    * @ORM\Column(name="name", type="string", length=255)
    *
    */
   private $name;

   /**
    * @var string
    *
    * @ORM\ManyToOne(targetEntity="Field", inversedBy="options")
    * @ORM\JoinColumn(name="field", referencedColumnName="name")
    */
   private $field;

// getter and setter methods...
}


//Form/CaseType.php
class CaseType extends AbstractType
{
   /**
    * @param FormBuilderInterface $builder
    * @param array $options
    */
   public function buildForm(FormBuilderInterface $builder, array
$options)
   {

       $builder->add('fields', 'collection', array(
                       'type'          => new FieldType($this->em),
                   ));
   }
// ...
}

//Form/FieldType.php
class FieldType extends AbstractType
{
   /**
    *
    * @param FormBuilderInterface $builder
    * @param array $options
    */
   public function buildForm(FormBuilderInterface $builder, array
$options)
   {


       $formFactory = $builder->getFormFactory();
       $builder->addEventListener(FormEvents::PRE_SET_DATA, function
(FormEvent $event) use ($formFactory) {
               $form = $event->getForm();
               $this->field = $event->getData();
                       $multi = false;
                       $exp = false;

               if($this->field->getType() == "C") {
                       $multi = true;
                       $exp = true;
               }
               elseif($this->field->getType() == "E") {
                       $multi = false;
                       $exp = false;
               }
               elseif($this->field->getType() == "J") {
                       $multi = false;
                       $exp = true;
               }
               elseif($this->field->getType() == "R") {
                       $multi = false;
                       $exp = true;
               }
               else {
                       dump($this->field);
                       return;
               }
               $form->add('options', 'entity', array(
                   "multiple"  => $multi,
                   "expanded"  => $exp,
                   "class"     => "AcmeMyBundle:Option",
                   "property"  => "name",
                   "label"     => $this->field->getLabel(),
                   "choices"   => $this->field->getOptions(),
               ));
       });
   }
}



What troubles me is how to save everything on the case entity and load
it correctly when generating the form. Can someone point me to the
direction how to do this correctly with doctrine? And is the setup
correct like this or should the structure of the database be different
to accomplish this?

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to