CakePHP 2.0 Model :: read()

时间:2012-07-27 03:22:02

标签: cakephp

我有一个包含2个字段的表 TableName:组 字段: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>

我可以调用/cake文件/index.php/groups它列出数据库中的所有组,当我点击它们中的任何一个时,url变成/cakephp/index.php/groups/view/1(1是group_id)我看不到视图上的任何数据(根本没有错误)。

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

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

感谢。 Kongthap

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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';

}