2013-04-20 52 views
0

我得到表單的內容寫入數據庫。我的代碼如下:CakePHP:將表單內容寫入數據庫時​​出現問題

視圖(index.ctp):

<div class="modal fade" id="test_modal"> 
     <div class="modal-header"> 
     <a class="close" data-dismiss="modal">&times;</a> 
     <h1>Create Customer</h1> 
     </div> 
     <div class="modal-body"> 
     <?php 
     echo $this->Form->create('Contact'); 
     echo $this->Form->input('type'); 
     echo $this->Form->input('name'); 
     echo $this->Form->input('company'); 
     echo $this->Form->input('phone'); 
     echo $this->Form->input('mobile'); 
     echo $this->Form->input('email'); 
     echo $this->Form->input('vatNumber'); 

     echo $this->Form->input('mainAddressLine1'); 
     echo $this->Form->input('mainAddressLine2'); 
     echo $this->Form->input('mainAddressTown'); 
     echo $this->Form->input('mainAddressCounty'); 
     echo $this->Form->input('mainAddressPostCode'); 
     echo $this->Form->input('mainAddressCountry'); 

     echo $this->Form->input('notes', array('rows' => '5')); 
     echo $this->Form->end('Save Customer'); 
     ?> 
     </div> 
    </div> 

控制器(ContactController.php)

class ContactsController extends AppController { 
    public $helpers = array('Html', 'Form', 'Session'); 
    public $components = array('Session'); 

    public function index() { 
     $this->set('contacts', $this->Contact->find('all')); 
    } 

    public function view($id) { 
     if (!$id) { 
      throw new NotFoundException(__('Invalid contact')); 
     } 

     $contact = $this->Contact->findById($id); 
     if (!$contact) { 
      throw new NotFoundException(__('Invalid contact')); 
     } 
     $this->set('contact', $contact); 
    } 

    public function add() {     
     if ($this->request->is('post')) { 
      $this->Contact->create(); 
      if ($this->Contact->save($this->request->data)) { 
       $this->Session->setFlash('Your contact has been saved.'); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash('Unable to add your contact.'); 
      } 
     } 
    } 
} 

模型(Contact.php)

class Contact extends AppModel {  
} 

任何幫助讚賞。

問候, 斯蒂芬

回答

0

如果視圖文件是index.ctp,這意味着,在默認情況下,邏輯thta的處理,因爲這是在控制器的index()方法。該方法只設置一個變量。我假設你正在混合視圖/動作。請嘗試在add.ctp視圖中添加您的表單,並調用contacts/add URI。

或者您可以覆蓋通過添加一個$this->render調用您的add()方法呈現的觀點:

public function add() { 
    $this->autoRender = false; 
    // Rest of your logic 
    $this->render('index'); 
} 

這樣的contacts/add URI將服務於index.ctp視圖,而不是add.ctp

+0

謝謝@Oldskool,是的,我已經添加了代碼add.ctp,它現在可以工作。 – Stephen 2013-04-20 21:39:57

相關問題