我有一个与表 UserDetail 相关的用户表。
User(#id, name, password)
UserDetail(#id, address, city, user_id)
UserDetail.user_id
是链接到的外键User.id
。
如果我想添加一个新地址,这是我填写地址的表格:
<?php echo $this->Form->create('UserDetail');?>
<?php echo $this->Form->hidden('id'); ?>
<?php echo $this->Form->input('address', array('class' => 'form-control')); ?>
<?php echo $this->Form->input('city', array('class' => 'form-control')); ?>
<?php echo $this->Form->hidden('user_id'); ?>
<?php echo $this->Form->button('Modifier', array('class' => 'btn btn-primary')); ?>
<?php echo $this->Form->end(); ?>
我的控制器:
public function customer_edit($id = null){
if (!$this->User->exists($id)) {
throw new NotFoundException('Invalid user details');
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->request->data['UserDetail']['user_id'] = $id;
if ($this->User->UserDetail->save($this->request->data)) {
$this->Flash->success('Done!');
return $this->redirect(array('action' => 'dashboard'));
} else {
$this->Session->setFlash('Error.');
}
}
}
而且我总是有闪存“错误”。if ($this->User->UserDetail->save($this->request->data))
不起作用。如果我看一下$request->data
DebugKit,我有所有的输入数据。
添加新记录不起作用...问题出在哪里?
谢谢。
编辑:这是我的 UserDetail 类:
class UserDetail extends AppModel {
public $validate = array(
'id' => array(
'numeric' => array(
'rule' => array('numeric')
),
),
'address' => array(
'notempty' => array(
'rule' => array('notempty'),
'allowEmpty' => false,
),
),
'city' => array(
'notempty' => array(
'rule' => array('notempty'),
'allowEmpty' => false,
),
),
'user_id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
);
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => '',
'counterCache' => true,
'counterScope' => array(),
)
);
}
和 UserDetail 发布数据内容:
>用户详情
ID
地址测试
城市测试测试
用户 ID 19