Alberto Mnemon
Alberto Mnemon

Reputation: 117

symfony choice options loaded from a table

i need some help with Symfony and Doctrine forms.

What i want is to add a choice widget to a form, and load that choice widget with data from the db.

I have this in Survey.class.php:

$this->widgetSchema['service'] = new sfWidgetFormChoice(array(
  'choices'  => TService::getServiceTypes(),
  'multiple' => true,
  'expanded' => false));

And this is TService.class.php

class TService extends BaseTService
{
  static public function getServiceTypes()
  {
    return Doctrine::getTable("TService")
       ->createQuery()
       ->select('description')
       ->execute();
  }
}

What i have got is a select with 2 choices, and that's good, but the options are '1' and '2', instead of the service's descriptions.

Sugestions?

Thank you guys

Upvotes: 1

Views: 2738

Answers (4)

Alberto Mnemon
Alberto Mnemon

Reputation: 117

i got a "manual" solution, but I'm sure there are better ways. I just changed the TService::getServiceTypes method

$table = Doctrine::getTable("TService")
   ->createQuery()
   ->select('description')
   ->execute();

$arrayServices = $table->toArray();
$descServices = array();
foreach ($arrayServices as $service){
    $descServices[] = $service['description'];
}

return $descServices;

Upvotes: 0

Maerlyn
Maerlyn

Reputation: 34107

You should use sfWidgetFormDoctrineChoice instead of manually fetching the options (it has a validator pair as well). However, you'll still have your current problem. The solution for that: implement the __toString() magic function for your TService objects.

Upvotes: 1

yitznewton
yitznewton

Reputation: 810

When you do ./symfony doctrine:build --all, it will create sfWidgetFormDoctrineChoice automatically, if you have the right schema:

Survey:
  columns:
    tservice_id: { type: integer }
  relations:
    TService:
      class: TService
      local: tservice_id
      foreignAlias: Surveys

TService:
  columns:
    description: { type: string(100) }

Upvotes: 0

PurplePilot
PurplePilot

Reputation: 6612

Read this populating drop downs from the database hopefully even if it is not what you want it will help. It's S 1.4 and Doctrine based.

Upvotes: 0

Related Questions