try this:-
Group Model:--
<?php
App::uses('AppModel', 'Model');
/**
* Group Model
*
* @property Group $ParentGroup
* @property Group $ChildGroup
* @property User $User
*/
class Group extends AppModel {
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
/**
* Display field
*
* @var string
*/
public $displayField = 'name';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'name' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'ParentGroup' => array(
'className' => 'Group',
'foreignKey' => 'parent_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'ChildGroup' => array(
'className' => 'Group',
'foreignKey' => 'parent_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'group_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
Group Controller:--
<?php
App::uses('AppController', 'Controller');
/**
* Groups Controller
*
* @property Group $Group
*/
class GroupsController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
}
/**
* admin_index method
*
* @return void
*/
public function admin_index() {
$this->Group->recursive = 0;
$this->set('groups', $this->paginate());
}
/**
* admin_view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function admin_view($id = null) {
$this->Group->id = $id;
if (!$this->Group->exists()) {
throw new NotFoundException(__('Invalid group'));
}
$this->set('group', $this->Group->read(null, $id));
}
/**
* admin_add method
*
* @return void
*/
public function admin_add() {
if ($this->request->is('post')) {
$this->Group->create();
if ($this->Group->save($this->request->data)) {
$this->Session->setFlash(__('The group has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
}
}
$parentGroups = $this->Group->ParentGroup->find('list');
$this->set(compact('parentGroups'));
}
/**
* admin_edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function admin_edit($id = null) {
$this->Group->id = $id;
if (!$this->Group->exists()) {
throw new NotFoundException(__('Invalid group'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Group->save($this->request->data)) {
$this->Session->setFlash(__('The group has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Group->read(null, $id);
}
$parentGroups = $this->Group->ParentGroup->find('list');
$this->set(compact('parentGroups'));
}
/**
* admin_delete method
*
* @throws MethodNotAllowedException
* @throws NotFoundException
* @param string $id
* @return void
*/
public function admin_delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Group->id = $id;
if (!$this->Group->exists()) {
throw new NotFoundException(__('Invalid group'));
}
if ($this->Group->delete()) {
$this->Session->setFlash(__('Group deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Group was not deleted'));
$this->redirect(array('action' => 'index'));
}
}
Group view:--
admin_index.ctp file
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('parent_id'); ?></th>
<th><?php echo $this->Paginator->sort('name'); ?></th>
<th><?php echo $this->Paginator->sort('created'); ?></th>
<th><?php echo $this->Paginator->sort('modified'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php
foreach ($groups as $group): ?>
<tr>
<td><?php echo h($group['Group']['id']); ?> </td>
<td>
<?php echo $this->Html->link($group['ParentGroup']['name'], array('controller' => 'groups', 'action' => 'view', $group['ParentGroup']['id'])); ?>
</td>
<td><?php echo h($group['Group']['name']); ?> </td>
<td><?php echo h($group['Group']['created']); ?> </td>
<td><?php echo h($group['Group']['modified']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $group['Group']['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $group['Group']['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $group['Group']['id']), null, __('Are you sure you want to delete # %s?', $group['Group']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>