removing all fields from a symfony form

I’m making a good bit of headway into the new platform’s website for work and am starting to finally get deeper into the codebase for symfony.  I have a couple tables in my db schema that are *ahem* lengthy.  They are roughly 200 columns and symfony’s default behavior when removing fields from a form are not conducive to wholesale changes to a form.

I googled for a solution, hoping there would be  symfony feature that would alleviate the time consuming process of removing close to 200 fields from a form.  I came across Kamil Adryjanek’s blog which showed how to code a great little function to remove all the fields from a form except the fields you pass into it.  Fantastic!  And in his comments someone reworked the code to work with Doctrine.

Below is the rather simple and straightforward code:

<

//  lib/form/BaseFormPropel.class.php
 
public function unsetAllExcept($fields = array())
{
     foreach($this->getObject()->toArray(BasePeer::TYPE_FIELDNAME) as $key => $val)
     {
          $tmp[] = strtolower($key);
     }
     $tmp = array_diff($tmp, $fields);
 
     foreach($tmp as $values)
     {
          unset($this[$values]);
     }
}
 
//  lib/form/ActivitySeismoInfoPeer.class.php
public function configure()
{
     $this->unsetAllExcept(array('asi_seismo_id', 'asi_seismo_operation'));
}

Till Next  Time

Tags: , ,

Leave a Reply