2012-08-07 36 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