无法将数据从 CakePHP 2.5.1 中的一个表单保存到多个表中
Can't Save data into multiple tables from one form in CakePHP 2.5.1
我是 CakePHP 的新手,正在使用 CakePHP 2.5.1 和 MySQL 开发云应用程序。我有以下 tables:
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
引用相同 table people
的 primary_key people.id
。 role
的值可以是 manager
或 customer
。如果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.')
);
}
}
问题是前三个字段的数据(在用户和人员 table 中)被保存,其他数据(地址 table)没有被保存。
谁能告诉我我在这里错过了什么?
模型 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'=>true
添加到 saveAll()
:
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.')
);
}
}
我是 CakePHP 的新手,正在使用 CakePHP 2.5.1 和 MySQL 开发云应用程序。我有以下 tables:
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
引用相同 table people
的 primary_key people.id
。 role
的值可以是 manager
或 customer
。如果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.')
);
}
}
问题是前三个字段的数据(在用户和人员 table 中)被保存,其他数据(地址 table)没有被保存。
谁能告诉我我在这里错过了什么?
模型 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'=>true
添加到 saveAll()
:
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.')
);
}
}