【问题标题】:Can't Save data into multiple tables from one form in CakePHP 2.5.1无法在 CakePHP 2.5.1 中将数据从一种形式保存到多个表中
【发布时间】:2016-01-26 09:21:51
【问题描述】:

我是 CakePHP 的新手,正在使用 CakePHP 2.5.1 和 MySQL 开发一个云应用程序。我有以下表格:

users

  id ,
  person_id(FK- people.id),
  username,
  password

people

  id, 
  parent_id(FK- people.id), 
  firsr_name,
  last_name,
  role

addresses

  address_id,
  person_id(FK- people.id),
  street,
  house no,
  post code,
  city

外键people.parent_id指的是同一张表peopleprimary_key people.idrole 的值可以是 managercustomer。如果manager,则people.id 的值将分配给people.parent_id

在我的注册表单中,会有以下输入字段:

-username
-password
-first name
-last name
-street
-house no
-post code
-city
-role  

在视图 (view/Users/add.ctp) 中,我有以下字段:

         $options = array('manager' => 'Manager', 'customer' => 'Customer');
              $attributes = array('legend' => false); 
              echo $this->Form->radio('Person.role', $options, $attributes);    


              echo $this->Form->input('User.username');
              echo $this->Form->input('Person.last_name');
              echo $this->Form->input('Person.first_name');

              echo $this->Form->input('Address.street');
              echo $this->Form->input('Address.house_no');
              echo $this->Form->input('Address.post_code');
              echo $this->Form->input('Address.city');

UsersController中的动作:

 public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->saveAll($this->request->data)) {
                $components = array('Session'); 
                $this->Session->setFlash('Welcome !');
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        }
    }

问题是前三个字段(在用户和人员表中)的数据被保存,其他数据(在地址表中)没有保存。

谁能告诉我我在这里缺少什么?

【问题讨论】:

  • 可能是因为模型 User 与模型 Address 没有直接关系。为了帮助提供修复,请发布Users::add() 的代码。
  • 你的意思是我应该从 UsersController 或 add.ctp 发布 add() 操作的代码?
  • 是的,抱歉打错了。

标签: mysql cakephp orm cakephp-2.x


【解决方案1】:

模型User 与模型Address 没有直接关系。但是,它是通过模型Person 关联的,其中Person hasMany Address

试试下面的。

将您的视图更改为:

    $options = array('manager' => 'Manager', 'customer' => 'Customer');
    $attributes = array('legend' => false);
    echo $this->Form->radio('Person.role', $options, $attributes);


    echo $this->Form->input('User.username');
    echo $this->Form->input('Person.last_name');
    echo $this->Form->input('Person.first_name');

    echo $this->Form->input('Person.Address.0.street');
    echo $this->Form->input('Person.Address.0.house_no');
    echo $this->Form->input('Person.Address.0.post_code');
    echo $this->Form->input('Person.Address.0.city');

并在您的UsersController.add() 中添加'deep'=>truesaveAll()

public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->saveAll($this->request->data,['deep'=>true])) {
            $components = array('Session'); 
            $this->Session->setFlash('Welcome !');
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(
            __('The user could not be saved. Please, try again.')
        );
    }
}

【讨论】:

  • 它将是数组('deep' => true)。谢谢你,伙计!它现在正在工作。正如我们之前所讨论的,缺少一件事,对于经理,People.id 应该分配给 People.Parent_id。但它没有发生。
  • 没错,就是 ['deep'=>true] 短数组语法。关于parent_id,在输入客户时必须填写此字段,并且必须是现有的Manager.idPerson.id的别名)。您可以将此值作为参数传递给add() 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多