Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: Adding a collection based on a table-inheritance structure to a FormView

I am working on a Symfony2/Doctrine app which uses class-table-inheritance (http://docs.doctrine-project.org/en/2.0.x/reference/inheritance-mapping.html#class-table-inheritance) to manage Complaints in a Consult. Each Consult can have many Complaints (OneToMany), and each different type of Complaint has a different structure and appearance. Complaints are a collection and are added dynamically with JS.

At this point, I am able to persist the Complaints and link them to the Consults by recasting them as the appropriate types in the Controller before persistence. I have run into some issues with this and I'm planning on migrating this to a form event (http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html) or something of that nature to streamline the process.

The problem that I am running into at this point, however, is that I am unable to display existing Complaints in a view using the FormView because the form builder demands that I set the type of the collection to be displayed. If each Consult had only one type of Complaint, that would be fine, but they can have multiple types, and setting the type in the form builder limits me to that one type.

Is there some approach that I can take to stop the FormView from tyring to convert to string in the absence of a type or some way to dynamically detect and assign the type on a per-Complaint basis (using $complaint->getComplaintType(), perhaps)?

<?php
namespace Acme\ConsultBundle\Entity;
class Consult
{
    /**
     * @ORM\OneToMany(targetEntity="Acme\ConsultBundle\Entity\ComplaintBase", mappedBy="consult", cascade={"persist", "remove"})
     */
    protected $complaints;
}
?>


<?php
namespace Acme\ConsultBundle\Entity;
/**
 * Acme\ConsultBundle\Entity\ConsultBase
 *
 * @ORM\Entity
 * @ORM\Table(name="ConsultComplaintBase")
 * @ORM\HasLifecycleCallbacks
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="complaint_name", type="string")
 * @ORM\DiscriminatorMap({
 *  "ComplaintDefault"      = "Acme\ConsultBundle\Entity\ComplaintDefault",
 *  "ComplaintRosacea"      = "Acme\ConsultBundle\Entity\ComplaintRosacea",
 *  "ComplaintBotox"        = "Acme\ConsultBundle\Entity\ComplaintBotox",
 *  "ComplaintAcne"         = "Acme\ConsultBundle\Entity\ComplaintAcne",
 *  "ComplaintUrticaria"    = "Acme\ConsultBundle\Entity\ComplaintUrticaria",
 * })
 */
abstract class ComplaintBase
{
    /**
     * @ORM\ManyToOne(targetEntity="Acme\ConsultBundle\Entity\Consult", inversedBy="complaints")
     * @ORM\JoinColumn(name="consult_id", referencedColumnName="id")
     */
    protected $consult;
    /**
     * @ORM\Column(type="string", length="255")
     */
    protected $complaintType;
}
?>


<?php
namespace Acme\ConsultBundle\Form\Type;
class ConsultType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('complaints', 'collection', array(
                // 'type' => new ComplaintUrticariaType(),
                'error_bubbling' => true,
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
            ));
    }
}
?>
like image 424
amsross Avatar asked Dec 06 '12 19:12

amsross


1 Answers

Not exactly sure that it will work with the collection, but it certainly works with a single form. Please try this idea.

First make a form for your basic entity ComplaintBase

class ComplaintForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $subscriber = new ComplaintSubscriber($builder);
        $builder->addEventSubscriber($subscriber);

        /* your fields */ 
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\ConsultBundle\Entity\ComplaintBase',
        ));
    }
}

Then in subscriber you can define additional fields based on submitted entity type.

class ComplaintSubscriber implements EventSubscriberInterface
{
    private $factory;
    private $builder;

    public function __construct(FormBuilderInterface $builder)
    {
        $this->factory = $builder->getFormFactory();
        $this->builder = $builder;
    }

    public static function getSubscribedEvents()
    {
        return array(
            FormEvents::PRE_SET_DATA => 'preSetData',
        );
    }

    public function preSetData(FormEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        if (null === $data) {
            return;
        }

        $class = get_class($data);
        if( $class === 'Acme\ConsultBundle\Entity\ComplaintDefault' ) {
            $this->processDefault($data, $form);
        }
        elseif( $class === 'Acme\ConsultBundle\Entity\ComplaintRosacea' ) {
            $this->processRosacea($data, $form);
        }
        elseif( $class === 'Acme\ConsultBundle\Entity\ComplaintBotox' ) {
            $this->processBotox($data, $form);
        }
        else {
            #NOP
        }
    }

    protected function processDefault(Entity\ComplaintDefault $node, FormInterface &$form)
    {
        #NOP
    }

    protected function processRosacea(Entity\ComplaintRosacea $node, FormInterface &$form)
    {
        $form->add($this->factory->createNamed('some_field', 'text'));
    }

    protected function processBotox(Entity\ComplaintBotox $node, FormInterface &$form)
    {
        $form->add($this->factory->createNamed('other_field', 'text'));
    }
}
like image 88
Alexey B. Avatar answered Nov 03 '22 04:11

Alexey B.