3

I am building a form collection and it output's the rows like this:

<input type="number" name="order[items][1][qty]">
<input type="number" name="order[items][2][qty]">

But what I want to do is have this bit:

<input type="number" name="order[items][DB_ID_1][qty]">
<input type="number" name="order[items][DB_ID_2][qty]">

DB_ID_1 would be a numeric value of the database record ID. So I can print out each row in my template like this:

{{ form.items.1 }}

So it prints the form element out for the database record with ID 1, currently it prints out the element for the key of 1 which could have the real database id of 9 for example

Any more information required please let me know. Thanks

2
  • I think I understand what you want to do. My question is: Why? Commented May 20, 2013 at 15:41
  • I want to go through a list of order items and print out the matching qty field input using something like {{ form.items.DB_ID }} - if there is a better method available I am open to suggestions - I am formatting it all inside a table, thanks Commented May 20, 2013 at 15:43

2 Answers 2

3

I have finally found the answer with the help of this post Embedding a collection of forms Symfony2 forms with adding and deleting allowed

It was such a simple solution - I was looking in the wrong place, I needed to use INDEX BY x.id in my custom Class Repository like so:

$em->createQuery("SELECT f FROM ExampleCoreBundle:Folder f INDEX BY f.id WHERE f.uid = :uid");

This returned my array of Folder objects back with the keys of the array being the database record's id field for the folder.

This now works fine with the Form Collections as I originally wanted - I am pretty sure this information is not on the Form Collections page on the website so hope this helps anyone else in future!

Sign up to request clarification or add additional context in comments.

Comments

1

you can access the value of the objects inside the form you passed to the view using:

{% for order in form.orders %}
  {{ order.vars.value.id }}
{% endfor %}

http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand

3 Comments

thanks - I know about that already, should have included in my question sorry, but this does not help me match the correct qty field to the right order item - unless I am missing something?
So then if you want the qty field next to the id you can use {{ order.vars.value.id }} : {{ order.vars.value.qty }}. Or is the order entity more complex than that?
{{ order.vars.data.id }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.