为什么我的模型没有被保存? (CakePHP 2.x)
Why is my model not being saved? (CakePHP 2.x)
我有一个与 table UserDetail 相关的用户 table。
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.');
}
}
}
而且我总是有闪光灯'Error'。 if ($this->User->UserDetail->save($this->request->data))
不起作用。如果我用 DebugKit 查看 $request->data
,我有所有输入数据。
添加新记录不起作用...问题出在哪里?
谢谢。
编辑:
这是我的 UserDetail class :
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 post 数据内容:
>用户详细信息
id
地址测试
城市测试测试
user_id 19
通常您不想为主键设置验证规则Model::id。但是,如果这样做,则必须仅在更新记录时强制执行它,而不是在创建记录时强制执行(在这种情况下它是空的)。
尝试按如下方式重写验证数组:
public $validate = array(
'id' => array(
'numeric' => array(
'rule' => array('numeric'),
'on' => 'update' //don't enforce rule on create
),
),
'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'),
),
),
);
顺便说一句,在同一规则中包含 'rule' => array('notEmpty')
和 'allowEmpty' => false
是多余的。后者可以删除。
话虽如此,我还是建议您重新考虑一下您的观点和行动,因为它们的表现似乎并不像我认为的那样。
我有一个与 table UserDetail 相关的用户 table。
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.');
}
}
}
而且我总是有闪光灯'Error'。 if ($this->User->UserDetail->save($this->request->data))
不起作用。如果我用 DebugKit 查看 $request->data
,我有所有输入数据。
添加新记录不起作用...问题出在哪里?
谢谢。
编辑: 这是我的 UserDetail class :
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 post 数据内容:
>用户详细信息
id
地址测试
城市测试测试
user_id 19
通常您不想为主键设置验证规则Model::id。但是,如果这样做,则必须仅在更新记录时强制执行它,而不是在创建记录时强制执行(在这种情况下它是空的)。
尝试按如下方式重写验证数组:
public $validate = array(
'id' => array(
'numeric' => array(
'rule' => array('numeric'),
'on' => 'update' //don't enforce rule on create
),
),
'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'),
),
),
);
顺便说一句,在同一规则中包含 'rule' => array('notEmpty')
和 'allowEmpty' => false
是多余的。后者可以删除。
话虽如此,我还是建议您重新考虑一下您的观点和行动,因为它们的表现似乎并不像我认为的那样。