2013-11-25 86 views
0

早上好。
我在這裏向Symfony 1.4徵集關於嵌入式表格的社區。
我有兩種型號,關聯一對多
I 生成第一個模型及其所有依賴關係之前,顯示錶格
我總是得到這些錯誤:
modelb: Unexpected extra form field named "0". Unexpected extra form field named "1". Unexpected extra form field named "2". Unexpected extra form field named "3". Unexpected extra form field named "4". Unexpected extra form field named "5". Unexpected extra form field named "6". Unexpected extra form field named "7". Unexpected extra form field named "8". Unexpected extra form field named "9". Unexpected extra form field named "10". Unexpected extra form field named "11". 我也試圖與embedRelation()和我一樣。 這裏有兩個模型模式摘要:
SF 1.4 embedform中的意外額外表單域(更新表)

ModelA: 
    columns: 
    model_d_id: { type: integer, notnull: true } 
    comments: { type: string(1024), notnull: false } 
    relations: 
    ModeleD: ... 

ModelB: 
    columns: 
    model_a_id: { type: integer, notnull: true } 
    model_b_id: { type: integer, notnull: false } 
    model_c_id: { type: integer, notnull: true } 
    is_selected: { type: boolean, notnull: true } 
    relations: 
    ModelA: 
     class: ModelA 
     local: model_a_id 
     foreign: id 
     onDelete: CASCADE 
     foreignAlias: ModelB 
    ModelB: ... 
    ModelC: ... 

所以,形式我希望能夠以更新的ModelAcomments,並有is_selected複選框相關的ModelB每個條目目前的ModelA。
這裏是形式是什麼樣子:

[] modele_b_entry_name_1 
[] modele_b_entry_name_2 
[] modele_b_entry_name_3 

text 
[_______________________________] 

[ Submit ] 

這裏是代碼嵌入的形式

class ModelBForm extends BaseModelBForm 
{ 
    public function configure() 
    { 
    unset($this['model_a_id'], $this['model_b_id'], $this['model_c_id'], $this['updated_at']); 

    $model_a = $this->getObject(); 

    $this->setWidget('is_selected', new sfWidgetFormInputCheckbox(
     array('label' => $model_a->ModelC->getName()), 
     array() 
    )); 
    $this->setValidator('is_selected', new sfValidatorBoolean()); 
    } 
} 

這裏是收集以前的嵌入形式:

class ModelBCollectionForm extends sfForm 
{ 
    public function configure() 
    { 
    $all_model_b = Doctrine_Query::create() 
     ->from('ModelB p') 
     ->where('p.model_a_id = ?', $this->getOption('model_a_id')) 
     ->execute(); 

    foreach ($all_model_b as $index => $model_b) { 
     $form = new ModelBForm($model_b); 
     $this->embedForm($index, $form); 
    } 
    } 
} 

而現在全球表格

class ModelAForm extends BaseModelAForm 
{ 
    public function configure() 
    { 
     unset($this['created_at'], $this['model_d_id']); 

     $subForm = new ModelBCollectionForm(null, 
      array('model_a_id' => $this->getObject()->getId()) 
     ); 
     $this->embedForm('allmodelb', $subForm); 
    } 
} 

該模板只是簡單地顯示錶格。
因此,第一個動作顯示形式,與表的現有條目,然後當你提交表單,這裏是代碼:

public function executeCreate(sfWebRequest $request) 
{ 
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT)); 
    $this->form = new ModelAForm(); 
    var_dump($request->getParameter($this->form->getName())); 
    $this->form->bind($request->getParameter($this->form->getName())); 
    foreach($this->form->getErrorSchema()->getErrors() as $key => $error) 
    { 
    echo '<p>' . $key . ': ' . $error . '</p>'; 
    } 
    var_dump($this->form->getValues()); 
} 

第一var_dump()顯示了良好的DATAS一個完整的陣列。第二個是空的。
我正在工作幾個小時,但我沒有成功將表單與POST數據綁定

感謝您的幫助!

編輯:我試圖覆蓋綁定方法,但它不工作......除非我取消設置ModelB數據,但然後save()方法將執行INSERT而不是UPDATE。

回答

0

我解決了我的問題。
以下是解決方案:

public function executeCreate(sfWebRequest $request) 
{ 
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT)); 

    $model_a_array = $request->getParameter('model_a'); 
    $id_model_a = $model_a_array['id']; 
    $model_a = Doctrine::getTable('ModelA')->find($id_model_a); 
    $this->form = new ModelAForm($model_a); 

    $this->form->bind($request->getParameter($this->form->getName())); 
    if($this->form->isValid()) { 
    $this->form->save(); 
    } 
}