我有以下内容:
用户
团体
在组表中,我有三组:
员工、客户和 testGroup
现在,当我尝试添加新用户时,我使用以下表单:
正如你从这个表单中看到的,添加用户控制器注册了我所有的组!
但是,当我添加新用户时,我的用户被插入到用户表中,但组 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 )
哪个是对的。