我有以下内容:
用户
组
组表中的我有三组:
员工,客户和测试组
现在,当我尝试添加新用户时,我使用以下格式:
正如您从此表单中看到的,添加用户控制器会注册我的所有组!
但是,当我添加新用户时,我的用户被插入到Users表中,但组ID为null:
在我的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 )
哪个是正确的。
答案 0 :(得分:1)
尝试查看您的观点,可能是您的表单不正确。 当你进行调试$ this->应以这种方式回答数据:
Array (
[User] => Array
(
[username] => root
[password] => admin
[group_id] => 1
)
)