Ask your Symfony questions! Pay money and get answers fast! (more info)

Form getObject equivalent in an Embedded Relation Symfony



I am a bit stuck on an issue in a form with an Embedded Relation.

I am creating a scheduling application. There is an event and associated with that, is a list of people who are available to staff it. I want to be able to edit these as a 1 to many relation.

Here is a screen snap of the form:

[[LINK href=""]][[/LINK]]

The relation has three columns:

Staff Person Name Role Availability

Everything works fine, but I would like to make the first column (staff person's name) read-only. I don't want the scheduler to be able to change the person.

In a NON-embedded form, I would remove the field from the form and use the getObject() function to access the data directly. Example:

<?php echo $form->getObject()->getFullName() ?>

Here is the HTML for the embedded relation in my _form.php partial:

<?php foreach ($form['Schedule'] as $scheduleRow) : ?>
<td> <?php echo $scheduleRow->getObject()->getFullName() ?> </td>n
<td> <?php echo $scheduleRow['role_id'] ?> </td>
<td> <?php echo $scheduleRow['status_id'] ?> </td>
<?php endforeach ; ?>

However, the getObject() function doesn't exist for the embedded form variable ($scheduleRow). How can I get access to the object associated with the row?

Or, if there is another way to make the name read-only, that would work too.

Thanks for any suggestions you have!


Answers (1)


Benjamin Lévêque answers:

You can use this

<?php $object = $form->getObject() ?>
<?php foreach ($form['Schedule'] as $i => $scheduleRow) : ?>
<td><?php echo $object->Schedule[$i]->getFullName() ?></td>
<td><?php echo $scheduleRow['role_id'] ?> </td>
<td><?php echo $scheduleRow['status_id'] ?> </td>
<?php endforeach ; ?>

and you also need to remove the fullname widget from the embeded form

Steven Cogorno comments:

This is exactly what I needed. THANK YOU!