Alex Rivera | Logout

Symfony2 Forms and Polymorphic collections

Asked 2012-07-16T06:34:16.557
12

Im playing around with Symfony2 and Im abit unsure how Symfony2 handles Polymorphic collections in the View component. It seems that i can create an entity with a collection of AbstractChildren, but not sure how to what to do with it inside a Form Type class.

For example, I have the following entity relationship.

/**
 * @ORM\Entity
 */
class Order
{
    /**
     * @ORM\OneToMany(targetEntity="AbstractOrderItem", mappedBy="order", cascade={"all"}, orphanRemoval=true)
     * 
     * @var AbstractOrderItem $items;
     */
    $orderItems;  
    ...
}


/**
 * Base class for order items to be added to an Order
 *
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({
 *     "ProductOrderItem" = "ProductOrderItem",
 *     "SubscriptionOrderItem " = "SubscriptionOrderItem "
 * })
 */
class AbstractOrderItem
{
    $id;
    ...
}

/**
 * @ORM\Entity
 */
class ProductOrderItem  extends AbstractOrderItem
{
    $productName;
}

/**
 * @ORM\Entity
 */
class SubscriptionOrderItem extends AbstractOrderItem
{
    $duration;
    $startDate;
    ...
}

Simple enough, but when im create a form for my order class

class OrderType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('items', 'collection', array('type' => AbstractOrderItemType()));
    }
}

I am unsure how to handle this situation where you effectively need a different Form Type for each class of item in the collection?

Edit
Report

1 Answer

-1

In the example you have give, you would have to create different form class for those ProductOrder and SubscriptionOrder

class ProductOrderType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        //Form elements related to Product Order here
    }
}

and

class SubsciptionOrderType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        //Form elements related SubscriptionOrder here
    }
}

In your OrderType form class you add both these forms, like this

class OrderType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
         $builder->add('product',new ProductOrderType())
         $builder->add('subscription',new SubsciptionOrderType())
        //Form elements related to order here
    }
}

Now this adds the two forms SubsciptionOrderType,ProductOrderType to the main form OrderType . So later in the controller if you initialize this form you will get all the fields of the subscription and product forms with that of the OrderType.

I hope this answers your questions if still not clear please go through the documentation for embedding multiple forms here. http://symfony.com/doc/current/cookbook/form/form_collections.html

answered 2012-07-23T09:48:27.943

Your Answer