【发布时间】: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指的是同一张表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.')
);
}
}
问题是前三个字段(在用户和人员表中)的数据被保存,其他数据(在地址表中)没有保存。
谁能告诉我我在这里缺少什么?
【问题讨论】:
-
可能是因为模型
User与模型Address没有直接关系。为了帮助提供修复,请发布Users::add()的代码。 -
你的意思是我应该从 UsersController 或 add.ctp 发布 add() 操作的代码?
-
是的,抱歉打错了。
标签: mysql cakephp orm cakephp-2.x