Ok Cerad was right with his comment, and we must use the collection for this. At first this may seem silly, but itโs right. It took me a while to get around it.
So I had to create a ProductType, which is an arrayCollection and inserts each Product. (same as in documentation with Task and tags)
I used this:
$repository = $this->getDoctrine()->getRepository('ExampleBundle:Product'); $products = $repository->findAll(); $productCollection = new Products; foreach ($products as $product) { $productCollection->getProducts()->add($product); } $collection = $this->createForm(new ProductsType, $productCollection); return $this->render('ExampleBundle:Default:index.html.twig', array( 'collection' => $collection->createView() ));
Then in twig I do:
<div class="products"> {% for product in collection.products %} {{ form_row(product.id) }} {{ form_row(product.name) }} {{ form_row(product.description) }} <br clear="all" /> {% endfor %} </div>
The task is completed.
And even you can apply themes to each line as follows:
{% block _productsType_products_entry_name_row %} <div class="yourDivName">{{ block('form_widget') }}</div> {% endblock %} {% block _productsType_products_entry_description_row %} <div class="yourDivDescription">{{ block('form_widget') }}</div> {% endblock %}
Cool stuff!
source share