2012-07-27 38 views
0

我有2場 表名的表:組 領域:GROUP_ID,group_descCakePHP的2.0型號::閱讀()

我有一個非常簡單的模型 ...

class Group extends AppModel { 
    public $name = 'Group'; 
} 

我控制器 ...

class GroupsController extends AppController { 
    public $helper = array('Html', 'Form'); 

    public function index() { 
    $this->set('records', $this->Group->find('all')); 
    } 

    public function view($id = null) { 
    $this->Group->group_id = $id; 
    $this->set('record', $this->Group->read()); 
    } 
} 

我的觀點(view.ctp) ...

<h1>Group</h1> 

<h2>Group Description: <?php echo h($record['Group']['group_desc']); ?></h2> 
<h2>ID: <?php echo $record['Group']['group_id']; ?></h2> 

我可以調用/cakephp/index.php/groups它列出當我點擊其中的任何鏈接變成/cakephp/index.php/groups/view/1數據庫上的所有組(1是一個group_id)我看不到在視圖上的任何數據(根本沒有錯誤)。

我不認爲我錯過拼寫任何數據庫字段的名稱(如果我這樣做,我會得到錯誤)。

請幫助解決我的代碼或給我一些提示如何調試它。

謝謝。 Kongthap

+1

它'$ helpers'僅供參考 – tigrang 2012-07-27 07:10:59

回答

1

試試這個: 控制器代碼:

public function view($id = null) { 
    $this->set('record', $this->Group->read(null, $id)); 
} 

型號代碼:

class Group extends AppModel { 
    public $name = 'Group'; 
    public $primaryKey = 'group_id'; 

}

+0

謝謝,我真的得到了什麼我想要。 – Artisan 2012-07-27 05:19:31

+0

@Kongthap:如果這個答案解決了你的問題,考慮upvoting和標記答案爲接受獎勵奎師那的努力。 – 2012-07-27 08:38:20

1
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']); ?>&nbsp;</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']); ?>&nbsp;</td> 
     <td><?php echo h($group['Group']['created']); ?>&nbsp;</td> 
     <td><?php echo h($group['Group']['modified']); ?>&nbsp;</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>