0

我有一个包含 2 个字段的表 TableName:groups 字段:group_id,group_desc

我有一个非常简单的模型...

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 它列出了数据库中的所有组,当我单击其中任何一个时,url 变成 /cakephp/index.php/groups/view/1 (1 是 group_id)我看不到视图上的任何数据(完全没有错误)。

我不认为我错过了拼写任何数据库字段的名称(如果我这样做了,我会得到错误)。

请帮助修复我的编码或给我一些如何调试它的提示。

谢谢。孔塔

4

2 回答 2

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>
于 2012-07-27T03:42:12.547 回答
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';

}

于 2012-07-27T05:11:14.443 回答