Ask your Symfony questions! Pay money and get answers fast! (more info)

Dropdown is not populating properly. Symfony

  • SOLVED

I have a dropdown that needs to be populated from the database, but with a query, and the query is based on a post variable.

I know that sfWidgetFormDoctrineChoice has an option for passing a method, but I can't pass a method + parameter like that.

So I used the simple sfWidgetFormChoice with the choice value being the Doctrine getTable and the parameter.. as shown in the bottom of this email.. HOWEVER, the dropdown is not well populated. The dropdown is formed like this:

<option value="0">20 Off Code</option>
<option value="1">Special Sale</option>
<option value="2">30 Off</option>

This is incorrect, since the value for 20 Off Code is not 0 , is actually 1 so the index is wrong.

$this->widgetSchema['campaign_id'] = new sfWidgetFormChoice(array
(
'choices' => Doctrine::getTable('Campaign')->getCampaigns(sfContext::getInstance()->getUser()->getGuardUser()->Organization) // Organization expected = 2
));


What can I do ?

Thank you.


@vps httpdocs]# ./symfony doctrine:dql "from Campaign c"
>> doctrine executing dql query
DQL: from Campaign c
found 4 results
-
id: '1'
organization_id: '2'
name: '20 Off Code'
description: '20 Off with Code'
is_active: true
-
id: '2'
organization_id: '2'
name: 'Special Sale'
description: 'Special Sale Offer'
is_active: true
-
id: '3'
organization_id: '1'
name: '20 Off'
description: '20 Off with Code'
is_active: true
-
id: '4'
organization_id: '2'
name: '30 Off'
description: '30 Off with Code'
is_active: false

Answers (3)

2010-03-05

Francis Belanger answers:

By default doctrine does not index his collection with the primary key.

In your query add something like this

$q = Doctrine_Query::create()
->from('Campaign c INDEXBY c.id');

The INDEXBY keywork will put the id field as your array key and should make your dropdown work correctly.

please refer to this link for more info on the INDEX BY KEYWORD :

http://www.doctrine-project.org/documentation/manual/1_1/pl/dql-doctrine-query-language:indexby-keyword

2010-03-06

Nicolò Pignatelli answers:

Assuming you declare the form variable in an action, you can switch back to sfWidgetFormDoctrineChoice passing the post variable as an option, like this:


public function executeAction(sfWebRequest $request)
{
$post_variable => $request->getParameter('post_variable');
$this->form = new MyForm(array(), array('post_variable' => $post_variable));

}


and then you can use it in your form <em>configure()</em> function, like this:


public function configure()
{
$post_variable = $this->getOption('post_variable');
...
}


Sounds smoother to me.

2010-03-06

JuG answers:

Hi,

I think you should use the <em>sfWidgetFormDoctrineChoice</em> and its <em>query</em> option :


// in YourForm::configure();

public function configure()
{


// Build a query, as you would do to retreive the list of campaign
$organisation_id = sfContext::getInstance()->getUser()->getGuardUser()->Organization->id;
$query = Doctrine_Query::create()->from('Compain c')
->where('c.organism_id=?', $organisation_id)
->orderBy('c.name asc');


// pass this query to the widget
$this->setWidget('campaign_id',new sfWidgetFormDoctrineChoice(array('model'=>'Campaign','query'=>$query)));

}





You can even do that directly in the action :


// in YourForm::configure();
public function configure()
{
$this->setWidget('campaign_id',new sfWidgetFormDoctrineChoice(array('model'=>'Campaign')));
}



// in YourActions::executeStuff
public function executeStuff(sfWebRequest $request)
{
$form = new YourForm;

//...
$org_id = $this->getUser()->getGuardUser()->Organization->id;
$query = Doctrine_Query::create()->from('Campaign c')->where('organization_id', $org_id);
$form->getWidget('campaign_id')->setOption('query', $query);

//...
// $form->bind..

$this->form = $form;
}