我是CakePHP的新手,我正在尝试实现Simple Acl Controlled Application教程,我已经达到了尝试添加新用户和组的部分。。
我成功添加了组,但当我尝试添加新用户时,我会收到"该用户无法保存。请再试一次。"这是功能的一部分。
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
我注意到表单试图创建一个包含我创建的所有不同组的下拉框,但下拉框是空的,我创建了三个不同的组(管理员、响应者和志愿者)。
这是添加用户视图的副本。。
<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('group_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
</ul>
</div>
型号按要求:
<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $belongsTo = array('Group');
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
public $primaryKey = 'id';
public $validate = array(
'id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'password' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'group_id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
//old belongs to
// public $belongsTo = array(
// 'Group' => array(
// 'className' => 'Group',
// 'foreignKey' => 'group_id',
// 'conditions' => '',
// 'fields' => '',
// 'order' => ''
// )
// );
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public function beforeSave($options = array()) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
return true;
}
}
调试消息:
array(
'User' => array(
'password' => '*****',
'id' => '',
'username' => 'iwanjones'
)
)
如有任何帮助,我们将不胜感激。感谢
在视图中的表单中,创建id
字段作为输入字段(尽管通过Cake的automagic,它应该将其转换为隐藏输入)。创建新用户时,还没有id
。它将在保存(创建)记录时确定。在大多数应用程序中,这将由MySQL后端的AUTO_INCREMENT
功能来完成,该功能会选择第一个可用的"免费"id。
在添加新用户的情况下,因此不需要id
字段。只有当你想编辑现有用户并确保Cake通过设置其id来编辑正确的用户时,你才需要它。
此时,您在视图中设置了id字段,但它没有值,因为它是一个新用户。在您的模型中,您添加了一个验证规则,该规则要求id
字段为数字。但是,该值实际上是空的。你应该做两件事来让它发挥作用:
- 从添加视图中删除
echo $this->Form->input('id');
行 - 从模型中删除id字段的验证规则(验证主键字段是非常罕见的,因为Cake已经以正确的方式处理了这一点)
这样应该可以成功保存用户。