1

I'm using Symfony's 1.4 admin generator, and I have a question about a one to many relationship. If A has many B and B one A, I will see a listbox when editing B, that's ok, but I have to add a list (with multiple choices) of B when I edit an A. I have a solution but I don't no if it is the right way : I have modified the A form this way :

class AForm extends BaseAForm
{
  public function configure()
  {
    $this->widgetSchema['b_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'B'));
    $this->validatorSchema['b_list'] = new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'B', 'required' => false));
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['b_list']))
    {
      $this->setDefault('b_list', $this->object->B->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveBsList($con);

    parent::doSave($con);
  }

  public function saveBsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
   }

    if (!isset($this->widgetSchema['b_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object-Bs->getPrimaryKeys();
    $values = $this->getValue('b_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Bs', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
      {
      $this->object->link('Bs', array_values($link));
    }
   }
 }

I use the same code which is generated for a many-to-namy relation. Is there a better way ?

Thanks

Edit :

The schema

Agency:
  columns:
    id:
      type:               integer(4)
      primary:            true
      autoincrement:      true
    name:                  { type: string(255), notnull: true }
  relations:
    Consultants:
      refClass:           Consultant

Consultant:
  columns:
    if:
      type:               integer(4)
      primary:            true
      autoincrement:      true
    name:                  { type: string(255), notnull: true }
    ref_agency:           integer(4)
  relations:
    Agence:
      foreignAlias:       Agencies
      local:              ref_agency
      foreign:            id
      class:              Agency
3
  • could you show us the schema ? you can use class and refClass in each relation in schema.yml to make it a one-to-many relation
    – HQM
    Commented Jan 22, 2011 at 0:51
  • thank you for your answer. The post is updated with the schema
    – fego
    Commented Jan 25, 2011 at 16:54
  • So is Agency A, and Consultant B? The schema doesn't match the code sample.
    – Nathan
    Commented Jan 25, 2011 at 17:23

1 Answer 1

0

You can install the plugin sfFormExtraPlugin and use the sfWidgetFormSelectDoubleList:

 public function configure()
  {
    $this->widgetSchema['b_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
  }

You don't need anything more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.