Note that there are some explanatory texts on larger screens.

plurals
  1. POComplex data structure using only the Form component
    primarykey
    data
    text
    <p>I'm developing a e-commerce system with customizable products. Each product may have some options that, for its part, may have one or many values selected by consumer. I can't use the variant approach due the high level of customization of my users (some products could have more than 1M of variants), so I need persist the option combination chosen by costumer.</p> <p>The form options are assembled dynamically once each product may have distinct options. This form should transform the user choice into a storable structure for a relational database.</p> <p>Basically, it's my scenario (my attempt):</p> <ul> <li>Product</li> <li>Option </li> <li>OptionValue</li> <li>ProductOption</li> <li>Order</li> <li>OrderItem</li> <li>OrderItemOption</li> </ul> <p>Fixture:</p> <ul> <li><strong>Option:</strong> 1#Salad <ul> <li><strong>Values:</strong> 1#Tomato, 2#Lettuce, 3#Pickles, 3#Carrot</li> </ul></li> <li><strong>Product:</strong> Hamburger</li> <li><strong>ProductOption:</strong> 1#Salad <ul> <li><strong>Values:</strong> 1#Tomato, 2#Lettuce, Picles</li> </ul></li> </ul> <p>My target is something like:</p> <pre><code>class OrderItemType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $field = $builder-&gt;create('options', new OptionPickerType(), ['options' =&gt; $options['product']-&gt;getOptions()]); $field-&gt;addModelTransformation(new FixOptionIndexTransformer()); $builder-&gt;add($field); } } class OptionPickerType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { foreach ($options['options'] as $productOption) { $name = $productOption-&gt;getId(); $builder-&gt;add($name, 'choice', array( 'choice_list' =&gt; new ObjectChoiceList($productOption-&gt;getValues(), 'label', array(), null, 'id'), 'multiple' =&gt; true, 'cascade_validation' =&gt; true, 'property_path' =&gt; '['.$name.']' )); } } } $form = $factory-&gt;create(new OrderItemType(), ['product' =&gt; $product]); if ($request-&gt;isMethod('POST')) { $form-&gt;bind($request); if ($form-&gt;isValid()) { $item = $form-&gt;getItem(); // A collection of ItemOption filled with the OptionValue picked out from Choice field } } </code></pre> <p>This configuration will return a collection of arrays of OptionValue as expected. In fact it isn't enough for my purposes. What I really need is a flatten collection with all chosen values more some extra data:</p> <pre><code>class ItemOption { protected $item; protected $productOption; protected $option; // $productOption-&gt;getName() protected $optionValue; protected $value; // / $optionValue-&gt;getLabel() } </code></pre> <p>As you can see, the value of Choice field in fact is inside the ItemOption. </p> <p>After some days trying, I could not figure out how to do this or even think in other approach.</p> <p>Can you help me?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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