Symfony partials and current object variables

The admin-generator is one of Symfony’s strongest and enticing aspects.  Knocking up an interactive view to add/edit/delete table records in a matter of minutes (or less if you don’t care about human readable values).

Despite the name, it’s even useful as a starting point for frontend modules. Though you will quite often have more going on in one edit screen than writing to just one table.

For instance, I have 2 classes: Receipt & ReceiptItem. This is a many to one relationship: many ReceiptItems to one Receipt.
In the backend, I have 2 separate admin screens. This allows direct access to  table records. But that’s far from user friendly in the frontend.

The screen I’m building in the frontend allows me to create a new Receipt record and add items to it at the same time.

When using Symfony 1.0, I could add a _ prefixed field name to the display option to have the admin-generated screen include a partial file (such as _receiptItems.php). You can, of course, still do this in Symfony 1.4. Except that whilst in Symfony 1.0 the current object is passed on to the partial (eg $receipt), in Symfony 1.4 this is only true for the list views.

The current object isn’t passed into the new or edit views. How irritating.

The way around this is to not add the call to the prefix in the generator.yml file, but actually call it from within a template file.  In this case it’s a matter of copying _form.php from the cache and then adding:

<?php include_partial('Receipt/receipt_items', array('receipt' => $receipt)); ?>

One thought on “Symfony partials and current object variables”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.