Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 Forms and Polymorphic collections
    primarykey
    data
    text
    <p>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.</p> <p>For example, I have the following entity relationship. </p> <pre><code>/** * @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; ... } </code></pre> <p>Simple enough, but when im create a form for my order class </p> <pre><code>class OrderType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder-&gt;add('items', 'collection', array('type' =&gt; AbstractOrderItemType())); } } </code></pre> <p>I am unsure how to handle this situation where you effectively need a different Form Type for each class of item in the collection?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload