2014-04-20 1 views
0

Я не могу добавить пользователя в мое приложение, потому что отсутствует group_id .. Я следовал этому руководству: http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html Выбор группы пуст и Мне нужно, чтобы добавить группу я созданные доCakePHP: Простое приложение, контролируемое с помощью Acl - Не позволяя мне добавлять нового пользователя, группы отсутствуют

моя модель/user.php

<?php 
App::uses('AppModel', 'Model'); 
App::uses('AuthComponent', 'Controller/Component'); 

class User extends AppModel { 
    public $belongsTo = array('Group'); 
    public $actsAs = array('Acl' => array('type' => 'requester')); 

    /**  Validation rules  */ 

    public $validate = array(
     'username' => array(
      'notEmpty' => array(
       'rule' => array('notEmpty'), 
       ), 
     ), 
     'password' => array(
      'notEmpty' => array(
       'rule' => array('notEmpty'), 
       ), 
     ), 
     'group_id' => array(
      'numeric' => array(
       'rule' => array('numeric'), 
       ), 
     ), 
    ); 

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

    public function beforeSave($options = array()) { 
     $this->data['User']['password'] = AuthComponent::password(
      $this->data['User']['password'] 
     ); 
     return true; 
    } 

    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)); 
     } 
    } 

} 

моя модель/Group.php

<?php 
App::uses('AppModel', 'Model'); 

class Group extends AppModel { 
    public $actsAs = array('Acl' => array('type' => 'requester')); 

    public $validate = array(
     'name' => array(
      'notEmpty' => array(
       'rule' => array('notEmpty'), 

      ), 
     ), 
    ); 

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

    public function parentNode() { 
     return 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('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> 
    </ul> 
</div> 

и мой контроллер/UsersController.php

<?php 
App::uses('AppController', 'Controller'); 
class UsersController extends AppController { 

public $components = array('Paginator'); 

public function index() { 
     $this->User->recursive = 0; 
     $this->set('users', $this->Paginator->paginate()); 
    } 


    public function view($id = null) { 
     if (!$this->User->exists($id)) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     $options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); 
     $this->set('user', $this->User->find('first', $options)); 
    } 

    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.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
      } 
     } 
    } 

public function edit($id = null) { 
     if (!$this->User->exists($id)) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     if ($this->request->is(array('post', 'put'))) { 
      if ($this->User->save($this->request->data)) { 
       $this->Session->setFlash(__('The user has been saved.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
      } 
     } else { 
      $options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); 
      $this->request->data = $this->User->find('first', $options); 
     } 
    } 
public function delete($id = null) { 
     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     $this->request->onlyAllow('post', 'delete'); 
     if ($this->User->delete()) { 
      $this->Session->setFlash(__('The user has been deleted.')); 
     } else { 
      $this->Session->setFlash(__('The user could not be deleted. Please, try again.')); 
     } 
     return $this->redirect(array('action' => 'index')); 
    } 

    public function login() { 
     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       return $this->redirect($this->Auth->redirect()); 
      } 
      $this->Session->setFlash(__('Your username or password was incorrect.')); 
     } 
    } 

    public function logout() { 
     //Leave empty for now. 
    } 

    public function beforeFilter() { 
     parent::beforeFilter(); 

     // For CakePHP 2.1 and up 
     $this->Auth->allow(); 
    } 
} 

ответ

1

В вас add.ctp, group_id поле пусто ... Вы должны изменить свое add действие как-

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.')); 
      return $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
     } 
    } 

    $groups = $this->User->Group->find('list'); // add this two lines to set group id 
    $this->set('groups', $groups); 
} 
Смежные вопросы