Symfony Generator Forms, Doctrine, and M:N Relationships

不想你离开。 提交于 2019-12-02 09:59:14

About 10 month ago I had similar trouble with M:N relationships and meta-data in the join table.

I found those blog entries of melikedev very useful! This is not exactly the same as your use case, but it might give you some hints:

http://melikedev.com/2009/12/09/symfony-w-doctrine-saving-many-to-many-mm-relationships/

http://melikedev.com/2009/12/06/symfony-sfformextraplugin-select-double-list-maintain-order/

http://melikedev.com/2010/04/06/symfony-saving-metadata-during-form-save-sort-ids/

first of all I can suggest an upgrade of symfony version - I use 1.4.11 where you'll have this functionality working from scratch.

If this is not possible in your case, the best place to this will be to override the base doSave() method in candidateForm like this:

protected function doSave($con = null)
{
    $existing = $this->object->Position->getPrimaryKeys();
    $values = $this->getValue('candidate_positions');

    $unlink = array_diff($existing, $values);
    $this->object->unlink('Position', array_values($unlink));

    $link = array_diff($values, $existing);
    $this->object->link('Position', array_values($link));

    parent::doSave($con);
}

and also you'll probably need to manually set the selected link object on form load like this:

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

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

This should do the trick.

UPDATE

I think that since the update to 1.4.11 doesn't help, there is some problem with the schema definition and my guess is that you need to add relation definition to link table 'candidatePosition' like this:

  relations:
    Candidate:
      class: Candidate
      local: candidate_id
      foreign: id
      foreignAlias: Candidates
    Position:
      class: Position
      local: position_id
      foreign: id
      foreignAlias: Positions

Hope this helps.

Regards.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!