cakephp数据库没有保存正确的信息

时间:2012-08-07 09:33:20

标签: database forms cakephp

我的函数将正确的值放入我的视图的表单中,但不保存数据库中的正确值。数据库正在保存accounts_users.id而不是accounts_users.account_id我的代码看起来应该输入人员accounts_users.account_id。我在表格

的下拉框中看到了正确的信息

添加功能

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 个答案:

答案 0 :(得分: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.');
  }
}