2013-07-22 41 views
0

我在嘗試將表單的信息保存到數據庫時遇到問題。即使在選定的網絡中爲每個影院手動設置劇院ID之後,我的表單似乎仍然無效。 這裏是我的模塊的的actions.class.php相關部分:我的表單有什麼問題?

這裏的executeCreate():

public function executeCreate(sfWebRequest $request) { 
    $this->form = $this->configuration->getForm(); 
    $this->showing = $this->form->getObject(); 
    $this->processCreateForm($request, $this->form); 
    $this->setTemplate('new'); 
} 

現在processCreateForm():

protected function processCreateForm(sfWebRequest $request, sfForm $form) { 
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); 

    $form_name = $form->getName(); 
    $parameters = $request->getParameter($form_name); 
    $network_id = $parameters['network_id']; 

    $theaters_list = Doctrine_Query::create() 
      [...] 
      ->execute(); 

    foreach ($theaters_list as $theater) { 
     $form->getObject()->setTheaterId($theater->theater_id); 
     $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); 

     if ($form->isValid()) { 
      $showing = $form->save(); 
     } else { 
      foreach ($form->getErrorSchema()->getErrors() as $key => $error) { 
       echo '<p>' . $key . ': ' . $error . '</p>'; 
      } 
     } 
    } 
    $this->getUser()->setFlash('update_success', true); 
    $this->setTemplate('new'); 
} 

這裏的輸出:

Theater_id required output

謝謝您的幫助

+0

請將您的形式和你的對象的架構在您的文章 – sinhix

回答

2

有兩個奇怪的事情會在這裏,我想破壞你的代碼。

  1. 您運行bind()方法兩次,這可能會重置對象上的值。

  2. 我不認爲getObject()方法通過引用返回對象。

所以,當你運行:

$form->getObject()->setX($val); 
    $form->save(); 

那麼你更新由形式返回的對象的字段,但是然後保存它仍然是綁定到形式的原始對象。

嘗試做這樣的事情:

$myObject = $form->updateObject()->getObject(); 
    $myObject->setX($value); 
    $myObject->save(); 

如果您使用表單編輯現有的對象,而不是創建一個新的updateObject()是很重要的。沒有這個,你會得到對象的舊值。

如果你想在循環中運行它,你只能循環設置和保存部分。所以,你會有這樣的事情在你的processCreateForm

protected function processCreateForm(sfWebRequest $request, sfForm $form) 
{ 
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); 

    if ($form->isValid()) { //You can check the validity of your form at this point. 

     //Create $theatersList 
     ... 

     $myObject = $form->updateObject(); 

     foreach ($theatersList as $theater) { 
      $myObject->setTheaterId($theater->theater_id); 
      $showing = $myObject->save(); 

      //Do something with $showing 

     } 
    } else { 
     //Print the errors. 
    } 
} 

使用此代碼,你可以取消設置在您的形式theatre_id小部件,因爲它不應該由用戶設置,並且不必是表單的一部分驗證。

編輯

一些修改代碼:

protected function processCreateForm(sfWebRequest $request, sfForm $form) 
{ 
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); 

    if ($form->isValid()) { //You can check the validity of your form at this point. 

     //Create $theatersList 
     ... 

     $myObject = $form->updateObject(); 
     $myObjectVars = $myObject->toArray(); 

     foreach ($theatersList as $theater) { 

      $myNewObject = new SomeClass(); 
      $myNewObject->fromArray($myObjectVars); 
      $myNewObject->setTheaterId($theater->theater_id); 
      $showing = $myNewObject->save(); 

      //Do something with $showing 

      $myNewObject->free(); 
      unset($myNewObject); 
     } 
    } else { 
     //Print the errors. 
    } 
} 
+0

謝謝,我確實忘了取消設置'theater_id'部件。我從這開始,並驗證了表單。我接受了您的建議,並將所有內容重新組織到'if($ form-> isValid()){}'條件中。謝謝。然而,該表已經有很多記錄,第一個新添加的元組的ID爲0,我不明白爲什麼,但我想這是另一個問題! – halpsb

+0

我的顯示錶格具有自動遞增的ID,這是該窗體中的隱藏字段。我現在添加了一個劇院列表,只能爲一個影院添加一個顯示。這完美地使用了'foreach()'中的2行,但我似乎無法使它適用於整個網絡。我相信我必須在'foreach()'中增加隱藏的顯示id的值。 '$ form-> getObject() - > getId()'只能在'$ showing = $ form-> save()'之後生效。我試着在'foreach'結尾增加,但是我得到一個錯誤? '完整性約束違規:1048'theater_id'列不能爲空' – halpsb

+0

嗯......也許你應該嘗試在每次保存前創建新的對象。如果我正確理解你的邏輯,你想填寫一個表格,提交一個行動,並根據這個表格創建與你找到的劇院一樣多的行。看到我的編輯回答,也許這將有所幫助。 –