Search code examples
symfony1symfony-1.4symfony-forms

Displaying choice based on foreign key / join


I have folowing schema:

Template

UserTemplate
  template_id

Costs
  template_id
  amount
  value

What I'm trying to do, is to create a sfWidgetFormDoctrineChoice that displays the shipping costs based on a UserTemplate id.

    $this->widgetSchema['cost'] = new sfWidgetFormDoctrineChoice(array(
      'model'      => 'Costs',
      'key_method' => 'getValue',
      'method'     => 'getAmount',
      'add_empty'  => 'Please Select Amount', 
      'expanded'   => false,
      'multiple'   => false
    ));

This displays all of the Costs.

Ideally, I'd like it to limit it to the UserTemplate.

I have looked at creating a custom query and passing that into the widget, but I'm not sure if this would be the correct way of doing this

So If I have a bunch of costs assigned to the template id of 12 and the user template references 12, when I'm on example.com/user-template/12 - I'd expect to see the costs for this in my form widget.


Solution

  • Creating a custom query and passing it to the widget is exaclty what you're looking for. You will have to build the query depending on the template_id you use in the URL.