Search code examples
symfony-1.4symfony-formsdoctrine-1.2

Symfony 1.4 embedRelation where clause


Is it possible to set a where clause on embedRelation?

$this->embedRelation('treatedStones');

I need to get the treatedStones where stone_free = 0


Solution

  • I think you can't do this with embedRelation, but you can do manually as it's done in sfFormDoctrine.

    $subForm = new sfForm();
    
    // create a custom query e.g. in `TreatedStoneTable::queryFree($relatedId)` and
    // a method to the class of `$this->getObject()` to retrieve free stones 
    // e.g. `Class::getFreeTreatedStones()` to call the query
    foreach ($this->getObject()->getFreeTreatedStones() as $index => $childObject)
    {
      $form = new TreatedStoneForm($childObject);
    
      $subForm->embedForm($index, $form);
      $subForm->getWidgetSchema()->setLabel($index, (string) $childObject);
    }
    
    $this->embedForm('treated_stones', $subForm);
    

    You can find further details about how forms work in the docs.