2011-01-21 10 views
1

私はSymfonyの1.4管理ジェネレータを使用しています.1対多の関係について質問があります。 AにBとBが1つ多くある場合は、Bを編集するときにリストボックスが表示されますが、それは問題ありませんが、Aを編集するとBのリストを複数選択する必要があります。 解決策がありますそれは正しい方法であれば何もしません:私はAを変更しているこの方法を形成:symfonyのadminジェネレータで1対多に

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)); 
    } 
    } 
} 

私は多くのツーnamy関係のために生成されているのと同じコードを使用します。 良い方法がありますか?

おかげ

編集:

スキーマ

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 
+0

あなたは私たちにスキーマを表示できますか? schema.ymlの各リレーションでclassとrefClassを使用して1対多の関係にすることができます – HQM

+0

あなたの答えに感謝します。投稿はスキーマ – fego

+0

で更新されました。エージェンシーAとコンサルタントBもそうですか?スキーマがコードサンプルと一致しません。 – Nathan

答えて

0

あなたはsfWidgetFormSelectDoubleListをプラグインsfFormExtraPluginをインストールして使用することができます

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

あなたはより多くの何も必要ありません。

関連する問題