首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP ACL教程group_id

CakePHP ACL教程group_id
EN

Stack Overflow用户
提问于 2012-04-30 10:05:08
回答 1查看 1.3K关注 0票数 3

我正在学习http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html教程,并且我在添加组和用户帐户的部分……

但是,当我访问添加新用户时,组的下拉列表是空的...会出什么问题呢?

下面是User.php模型的外观

代码语言:javascript
复制
<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 * @property Group $Group
 * @property Image $Image
 */
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
/**
 * Display field
 *
 * @var string
 */
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function beforeSave() {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        return true;
    }   
    public function bindNode($user) {
            return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
    }   

    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 $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id',
            'conditions' => '', 
            'fields' => '', 
            'order' => ''
            )   
        );  



    public $hasMany = array(
            'Image' => array(
                'className' => 'Image',
                'foreignKey' => 'user_id',
                'dependent' => false,
                'conditions' => '',
                'fields' => '',
                'order' => '',
                'limit' => '',
                'offset' => '',
                'exclusive' => '',
                'finderQuery' => '',
                'counterQuery' => ''
                )
            );

}

下面是Group.php模型的外观

代码语言:javascript
复制
<?php
App::uses('AppModel', 'Model');
/**
 * Group Model
 *
 * @property User $User
 */
class Group extends AppModel {
/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'name';
/**
 * Validation rules
 *
 * @var array
 */
        //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        return null;
    }   
    public $hasMany = array(
            'User' => array(
                'className' => 'User',
                'foreignKey' => 'group_id',
           'dependent' => false,
                'conditions' => '',
                'fields' => '',
                'order' => '',
                'limit' => '',
                'offset' => '',
                'exclusive' => '',
                'finderQuery' => '',
                'counterQuery' => ''
                )
            );

}

我的表看起来像这个'users‘表

代码语言:javascript
复制
>     > Field   Type    Null    Key Default Extra
>     > id  int(11) NO  PRI NULL    auto_increment
>     > username    varchar(64) NO  UNI NULL      password  varchar(82) NO      NULL     
>     > first_name  varchar(64) NO      NULL      last_name varchar(64) NO      NULL     
>     > created datetime    YES     NULL      group_id  int(11) NO      NULL

‘'groups’表

代码语言:javascript
复制
Field   Type    Null    Key Default Extra
id  int(11) NO  PRI NULL    auto_increment
name    varchar(100)    NO      NULL     
created datetime    YES     NULL     
modified    datetime    YES     NULL     

View/Users/add.ctp代码

代码语言:javascript
复制
<div class="users form">
<?php echo $this->Form->create('User');?>
    <fieldset>
        <legend><?php echo __('Add User'); ?></legend>
    <?php
        echo $this->Form->input('username');
        echo $this->Form->input('password');
        echo $this->Form->input('first_name');
        echo $this->Form->input('last_name');
        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>
        <li><?php echo $this->Html->link(__('List Groups'), array('controller' => 'groups', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Group'), array('controller' => 'groups', 'action' => 'add')); ?> </li>
        <li><?php echo $this->Html->link(__('List Images'), array('controller' => 'images', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Image'), array('controller' => 'images', 'action' => 'add')); ?> </li>
    </ul>
</div>

我创建了两个组。一个是“访问者”,另一个是“管理员”。下面是aros表现在的样子

代码语言:javascript
复制
id  parent_id   model   foreign_key alias   lft rght
        2   NULL    Group   4       1   2
        3   NULL    Group   5       3   4
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-30 11:34:03

你有没有先加群?首先,您必须添加组,然后尝试添加用户。

请验证您是否在用户模型User.php中设置了belongsTo关系

代码语言:javascript
复制
public $belongsTo = array(
    'Group' => array(
        'className' => 'Group',
        'foreignKey' => 'group_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

检查您的UserController Add方法是否包含以下代码

请验证$groups = $this->User->Group->find('list'); $this->set(compact('groups'));部件

代码语言:javascript
复制
    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.'));
        }
    }
    $groups = $this->User->Group->find('list');
    $this->set(compact('groups'));
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10377739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档