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

これを試してください:コントローラーコード:

 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 に答える
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 に答える