Transmission Line Noise

Pop­u­lat­ing a Sym­fony drop down from a database

Sun, Jan 16, 2011

I really didn’t like Sym­fony when i first used it but in the inter­ven­ing period it has grown up and so have i. I am cur­rently using ver­sion 1.4 and really get­ting to grips with it.

As i try to use more of the built in items it can get a bit tricky as some­times i don’t find ele­ment very intuitive.

What i wanted to do was use the built-in forms gen­er­a­tion and then manip­u­late the form ele­ments. This is as opposed to my more usual way of hand crank­ing forms which is of course time con­sum­ing and prone to error.

A lot of the infor­ma­tion on manip­u­lat­ing the wid­gets and over­rid­ing the val­i­da­tion were quite easy but what i really wanted to do was pop­u­late as select list from a dif­fer­ent table, a lookup as it were, and it did take me a while to crack it.

Of course once done it looks sim­ple and i feel stu­pid in tak­ing a cou­ple of hours to track it down and work it out. Any­way here is the the recipe on the of chance it can help some­one else.

I need a cus­tomer to reg­is­ter and in reg­is­ter­ing to select a price plan. The basic cus­tomer infor­ma­tion is in tna_customer with a row called pri­ce­plan which is the id of the selected pri­ce­plan from a sep­a­rate table called tna_priceplan.

I have gen­er­ated the mod­ule with the CRUD by running

./symfony doctrine:generate-module --with-show --non-verbose-templates frontend custreg TnaCustomer

But all i get when i nav­i­gate to the new form is a text box. The entry in the data­base schema id pri­ce­plan, int, 11.

This is because the gen­er­a­tor has given me an entry in the lib/form/doctrine/base class of

'priceplan'  => new sfWidgetFormInputText(),

This can then be over­rid­den with a wid­get helper called sfWid­get­Form­Choice. This is used in the /lib/form/doctrine entry for this form and looks like this.

 $this->widgetSchema['priceplan'] = new sfWidgetFormChoice(array( 'choices' => Doctrine_Core::getTable('TnaCustomer')->getPriceplans(), 'multiple' => false, 'expanded' => false, ));

And is essen­tially over­rid­ing the basic sfWid­get and is pro­vid­ing an array in the form.

The array that is brought in can come from a hard­wired array as fol­lows from /lib/model/doctrine (N.B. So you are using /lib/model/doctrine and the /lib/form/doctrine)

 static public $priceplan = array(
'full-time' => 'Full time',
'part-time' => 'Part time',
'freelance' => 'Freelance',
public function getPriceplans() {
 return self::$priceplan;

So to get the data from the data­base we need to extract the rel­e­vant infor­ma­tion and turn it into an array.

We do this as fol­lows in the /lib/model/doctrine class.php file


public static function getPricesplans() { $plan = array(); // initialise the array $q = Doctrine_Query::create() ->select(', pp.plan_name') ->from('TnaPriceplan pp'); $priceplan = $q->fetchArray(); // contract the doctrine query to return the id and the priceplan foreach ($priceplan as $pp) {  // build the array that i need $plan[$pp['id']] = $pp['plan_name']; } return $plan; // hand it back }
  Then we change the override to get this static function
 $this->widgetSchema['priceplan'] = new sfWidgetFormChoice(array( 'choices' => Doctrine_Core::getTable('TnaCustomer')->getPricesplans(), 'multiple' => false, 'expanded' => false, ));

And we get the con­tents of the pri­ce­plan in the drop down.