CakePHP ACL教程group_id
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了CakePHP ACL教程group_id,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4937字,纯文字阅读大概需要8分钟。
内容图文
![CakePHP ACL教程group_id](/upload/InfoBanner/zyjiaocheng/656/b17f827af2b7453c819d3d1fbed52c0e.jpg)
我正在关注
http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html教程,我在其中添加组和用户帐户的部分…
但是,当我访问添加新用户时,该组的下拉列表为空…有什么问题吗?
这是User.php模型的样子
<?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模型的样子
<?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' => ''
)
);
}
我的桌子看起来像这样
“用户”表
> > 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
“组”表
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
查看/用户/add.ctp代码
<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>
我创建了2个群组.一个是“访客”,一个是“管理员”.
这是现在的aros表的外观
id parent_id model foreign_key alias lft rght
2 NULL Group 4 1 2
3 NULL Group 5 3 4
解决方法:
您是否先添加了群组?首先,您必须添加组,然后尝试添加用户.
请确认您在用户模型User.php中建立了一个belongsTo关系
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’));部分
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'));
}
内容总结
以上是互联网集市为您收集整理的CakePHP ACL教程group_id全部内容,希望文章能够帮你解决CakePHP ACL教程group_id所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。