Mixing Object and Form Helpers in Symfony

Being new to Symfony I’m still getting my head around some of its paradigms. Two months with the manual before embarking on any development taught me that it was worth pursuing, but there’re always areas you need to see working.

The Object form helpers looked particularly useful, so for my first production development I aim to use them wherever possible. These helpers allow you edit objects directly, simplifying the update process back in your actions.

sample form in editformSuccess.php template:

<?php echo form_tag('editform/updatetitle'); ?>
<?php echo object_input_hidden_tag($newsletter, 'getID') ;?>
Title: <?php echo object_input_tag($newsletter, 'getTitle') ;?>
<?php echo submit_tag('update'); ?>

and in actions.class.php:

public function executeEditform($request)
$this->newsletter = »

public function executeUpdatetitle($request)
$nltoupdate = NewsletterPeer::retrieveByPk($request->getParameter(’id’));
$this->forward404Unless($nltoupdate );
return $this->redirect(»

The update code uses the fromArray method to update any properties of your object it holds data for, received from your form. Adding fields to your form doesn’t require changes to the action.

I got into difficulties when it came to setting a boolean field for my object, called “IsPublished”. Wishing to stick with the Object form helpers I investigated the Object_select_tag, however this has a different use altogether – allowing you to grab properties of other objects to match with your current object. The classic example being choosing an author from the authors object to match up to a post object.

I still wanted to save a property for my object, but I didn’t want to have it select from the object, or grab values from other objects. Further searches revealed I wasn’t alone in wondering how to set default selected values and generally get to grips with object_select_tag.

Mixing Object and Form helpers seemed the best solution, using select_tag for my boolean field. However this meant I wasn’t using the Object helper exclusively, so would it still work with fromArray?

The form code generated looked promisingly simple, so I tried a mix of object_input_tag and select_tag in my editformSuccess.php template:

$defaultOption = »
$newsletter->getIsPublished() == 1?1:0; # get current, set default
echo select_tag(’in_published‘, options_for_select(array(»
0, 1),

Note the highlighted property in select_tag. My Object Property is called “IsPublished”. To object-generate a plain text input field I’d have used:

Published: <?php echo object_input_tag($newsletter, 'getIsPublished') ;

So my current field value would be retrieved, and the Object helper generates form fields using BasePeer::TYPE_FIELDNAME which in turn is used in my fromArray method to update the Db.

Normally one would use select_tag with internal field names (BasePeer:: TYPE:PHP_NAME) and write more action code to marry them up before a save(), however here I have to add my database field name into my view code.

Tethering your templates to your database schema is a route no-one should go down. So the internal name should be converted to the Db name on-the-fly in your template. The Peer classes have a function for this, and its public. The following call converts your usual internal field name for you, ready to drop into your select_tag:

$IsPublished_translated = BaseNewsletterPeer::translateFieldName(»‘IsPublished’,BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME )

You could assign this in your action, but it works just as well in the template:

echo select_tag(»
‘IsPublished’,BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME‘,»
0, 1),

A final step would be to migrate this into a function higher up that so as to make templates a little prettier when editing.