2012-08-07 37 views
1

我的函數將正確的值放入我的視圖的窗體中,但不保存數據庫中的正確值。數據庫正在保存accounts_users.id而不是accounts_users.account_id我的代碼看起來應該是輸入人accounts_users.account_id。我看到在下拉框中正確的信息形式cakephp數據庫不保存正確的信息

附加功能

function add(){ 

    $accounts=$this->User->AccountsUser->find('list', array(
'fields'=>array('id','account_id'),'conditions' => array(
'user_id' => $this->Auth->user('id')))); 

    if($this->request->is('post')){ 
     $this->Template->create(); 

     if ($this->Template->save($this->request->data)) { 
     $this->Session->setFlash('The template has been saved'); 
     $this->redirect(array('controller' => 'Fields','action' => 'add')); 
     } else { 
     $this->Session->setFlash('The template could not be saved. Please, try again.'); 
     } 
    } 

這裏是添加視圖

<?php 
echo $this->Form->create('Template', array('action'=>'add')); 
echo $this->Form->input('name',array('label'=>'Template Name: ')); 
echo $this->Form->input('account_id',array('label'=>'Business: ', 'type' => 'select', 'options' => $accounts)); 
echo $this->Form->input('description',array('label'=>'Short Description Of Template: ')); 
echo $this->Form->end('Click Here To Submit Template'); 
?> 

回答

1

試試下面的代碼:

function add(){ 

$accounts=$this->User->AccountsUser->find('list', array(
'fields'=>array('account_id','account_id'),'conditions' => array(
'user_id' => $this->Auth->user('id')))); 

if($this->request->is('post')){ 
    $this->Template->create(); 

    if ($this->Template->save($this->request->data)) { 
    $this->Session->setFlash('The template has been saved'); 
    $this->redirect(array('controller' => 'Fields','action' => 'add')); 
    } else { 
    $this->Session->setFlash('The template could not be saved. Please, try again.'); 
    } 
} 
+0

我改變查詢中的'fields'選項。請檢查並詢問它是否對您無效。 – 2012-08-07 09:49:16

+0

它工作正常,謝謝!!!! – user1393064 2012-08-07 10:37:35