0

我是 CakePHP 的新手,我正在尝试实现Simple Acl Controlled Application tutorial,我已经到了尝试添加新用户和组的部分。

我成功添加了组,但是当我尝试添加新用户时,我收到“无法保存用户。请重试。” 部分功能。

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'
    )
)

任何帮助,将不胜感激。谢谢

4

1 回答 1

0

在您视图中的表单中,您将id字段创建为输入字段(尽管通过 Cake 的自动魔法它应该将其转换为隐藏输入)。创建新用户时,还没有id。将在保存(创建)记录时确定。在大多数应用程序中,这将由 MySQL 后端的AUTO_INCREMENT功能完成,它选择第一个可用的“免费”id。

在添加新用户的情况下,该id字段因此不是必需的。只有当您想要编辑现有用户并确保 Cake 编辑正确的用户时才需要它,方法是设置它的 id。

此时您在视图中设置了 id 字段,但它没有任何价值,因为它是一个新用户。id在您的模型中,您添加了一个要求字段为数字的验证规则。但是,该值实际上是空的。你应该做两件事来让它工作:

  1. echo $this->Form->input('id');从添加视图中删除该行。
  2. 从模型中删除 id 字段的验证规则(验证主键字段非常少见,因为 Cake 已经以正确的方式处理了这个问题)。

这应该允许用户成功保存。

于 2013-02-10T20:18:32.470 回答