【发布时间】:2013-08-13 12:20:40
【问题描述】:
我有以下:
用户
组
在组表中,我有三个组:
员工、客户和测试组
现在,当我尝试添加新用户时,我使用以下表单:
从这个表单中可以看到,添加用户控制器注册了我的所有组!
但是,当我添加新用户时,我的用户被插入到用户表中,但组 ID 为空:
在我的 Aros 中,parent_id 也为空。
现在这是我的用户模型:
class User extends AppModel {
public $belongsTo = array('Group');
public $actsAs = array('Acl' => array('type' => 'requester'));
function parentNode(){
if (!$this->id) {
return null;
}
$data = $this->read();
if (!$data['User']['group_id']){
return null;
} else {
return array('model' => 'Group', 'foreign_key' => $data['User']['group_id']);
}
}
public function bindNode($user) {
return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
}
// ...
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
这是我的用户控制器:
App::uses('Security', 'Utility');
class UsersController extends AppController {
public $components = array(
'Cookie',
'BloglicHelper',
'Session',
'HasOffers',
'RequestHandler'
);
function beforeFilter() {
$this->Auth->allow('add', 'index', 'login','logout');
}
function add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->saveAll($this->data)) {
$this->Session->setFlash(__('The User has been saved', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
}
}
$groups = $this->User->Group->find('list');
$this->set(compact('groups'));
}
谁能告诉我我做错了什么
更新 当我尝试从表单中打印数据时,我得到以下信息:
Array ( [User] => Array ( [username] => root [password] => admin ) [group_id] => 1 )
哪个是正确的。
【问题讨论】:
-
您是否尝试使用 save 功能代替 saveAll ?如果您的数据是 1 级深度,那么无论如何使用 saveAll 函数都是低效的。
-
是的,尝试了 save 和 saveAll