2014-04-09 83 views
0

這裏是應該實際發送形式的數據到數據庫中, 的方法,但我想不出爲什麼它不work.There沒有錯誤顯示出來,但數據未插入表。如果你能幫助我,我會很高興!ZendFramework2發佈形式

//method to add data to the database 
public function addAction() { 


     //add user info 
    $form = new UserForm(); 
    $form->get('submit')->setValue('add new info'); 

    $request = $this->getRequest(); 
    if($request->isPost()){ 
     $user = new User(); 
     $form->setData($request->getPost()); 

     if($form->isValid()){ 
      $user->exchangeArray($form->getData());//method that gets the validated data 
      $this->getUserTable()->saveUser($user); 

      return $this->redirect()->toRoute('application',array(
       'controller'=>'user', 
       'action'=>'index' 
      )); 
     } 
    } 

    //pass it to the view 
    $values = array('form'=>$form); 

    $view = new ViewModel($values); 
    return $view; 
} 

意見add.phtml

$form = $this->form; 

$form->setAttribute('action', $this->url(
       'application', array(
      'controller' => 'user', 
      'action' => 'add', 
       ) 
)); 
echo $this->form()->openTag($form); 
echo $this->formHidden($form->get('id')); 
echo $this->formRow($form->get('name')); 
echo $this->formRow($form->get('email')); 

echo $this->formSubmit($form->get('submit')); 
echo $this->form()->closeTag(); 
?> 

回答

1
$user = new User(); 
//user class contains the user information that you can store in the db 
$user->name = "Anime lover"; 
$user->email = "Anime mail"; 
$this->getUserTable()->saveUser($user); 

$view = new ViewModel($values); 
return $view; <- because of this return 

--------------你從來沒有經過此地----------- -----------

//add user info etc.... 
$form = new UserForm(); 

也許它是測試?是嗎?