0

ベイク スクリプトを使用してベイクした単純な cakephp 2.x アプリがあります。2 つの主要なテーブルは状態と薬です。状態 HABTM 薬とその逆。状態インデックスの状態に属する薬を表示しようとしています。基本的にはすべての状態をリストしていますが、それらの薬のみです。別の 1.3 アプリで動作するコンマ区切りのリストにある状態に属する薬を表示するために、次のようにしてみました。

<?php 
    $condition_drugs = '';
    foreach($condition['Drug'] as $drug):
      $condition_drugs .= $drug['drug'] . ', ';
      //echo $drug['generic'];
    endforeach;
    //echo substr($condition_drugs, 0, -2);
    echo $condition_drugs;
?>

ただし、次の 2 つのエラーが発生します: Undefined index: Drug [APP/View/Conditions/index.ctp、22 行目]、および foreach() に無効な引数が提供されました [APP/View/Conditions/index.ctp、22 行目]

また、同様の方法でそれを行うことを提案した別の記事も見ました。私が持っているコードに何か問題がありますか? ID に基づいてモデルを検索する必要がありますか? インデックス ビューの完全なコードとコントローラーは次のとおりです。

意見:

<div class="conditions index">
    <h2><?php echo __('Conditions'); ?></h2>
    <table cellpadding="0" cellspacing="0">
    <tr>
            <th><?php echo $this->Paginator->sort('condition'); ?></th>
            <th><?php echo $this->Paginator->sort('principles'); ?></th>
            <th><?php echo $this->Paginator->sort('treatment'); ?></th>
            <th><?php echo $this->Paginator->sort('clinical_tips'); ?></th>
            <th>Drugs</th>
            <th class="actions"><?php echo __('Actions'); ?></th>
    </tr>
    <?php
    foreach ($conditions as $condition): ?>
    <tr>
        <td><?php echo h($condition['Condition']['condition']); ?>&nbsp;</td>
        <td><?php echo h($condition['Condition']['principles']); ?>&nbsp;</td>
        <td><?php echo h($condition['Condition']['treatment']); ?>&nbsp;</td>
        <td><?php echo h($condition['Condition']['clinical_tips']); ?>&nbsp;</td>
    <td>
        <?php 
            $condition_drugs = '';
            foreach($condition['Drug'] as $drug):
              $condition_drugs .= $drug['drug'] . ', ';
              //echo $drug['generic'];
            endforeach;
            //echo substr($condition_drugs, 0, -2);
            echo $condition_drugs;
        ?>
    </td>
        <td class="actions">
            <?php echo $this->Html->link(__('View'), array('action' => 'view', $condition['Condition']['id'])); ?>
            <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $condition['Condition']['id'])); ?>
            <?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $condition['Condition']['id']), null, __('Are you sure you want to delete # %s?', $condition['Condition']['id'])); ?>
        </td>
    </tr>
<?php endforeach; ?>
    </table>
    <p>
    <?php
    echo $this->Paginator->counter(array(
    'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
    ));
    ?>  </p>

    <div class="paging">
    <?php
        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
        echo $this->Paginator->numbers(array('separator' => ''));
        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
    ?>
    </div>
</div>
<div class="actions">
    <h3><?php echo __('Actions'); ?></h3>
    <ul>
        <li><?php echo $this->Html->link(__('New Condition'), array('action' => 'add')); ?></li>
    </ul>
</div>

コントローラ:

<?php
App::uses('AppController', 'Controller');
/**
 * Conditions Controller
 *
 * @property Condition $Condition
 */
class ConditionsController extends AppController {

/**
 * index method
 *
 * @return void
 */
    public function index() {
    $this->set('title_for_layout','Condition Index');
        $this->Condition->recursive = 0;
        $this->set('conditions', $this->paginate());
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        $this->Condition->id = $id;
        if (!$this->Condition->exists()) {
            throw new NotFoundException(__('Invalid condition'));
        }
        $this->set('condition', $this->Condition->read(null, $id));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {
        if ($this->request->is('post')) {
            $this->Condition->create();
            if ($this->Condition->save($this->request->data)) {
                $this->Session->setFlash(__('The condition has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The condition could not be saved. Please, try again.'));
            }
        }
    $drugs = $this->Condition->Drug->find('list',array('fields'=>array('id','generic')));
    $this->set(compact('drugs'));
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function edit($id = null) {
        $this->Condition->id = $id;
        if (!$this->Condition->exists()) {
            throw new NotFoundException(__('Invalid condition'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Condition->save($this->request->data)) {
                $this->Session->setFlash(__('The condition has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The condition could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Condition->read(null, $id);
        }
    $drugs = $this->Condition->Drug->find('list',array('fields'=>array('id','generic')));
    $this->set(compact('drugs'));
    }

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Condition->id = $id;
        if (!$this->Condition->exists()) {
            throw new NotFoundException(__('Invalid condition'));
        }
        if ($this->Condition->delete()) {
            $this->Session->setFlash(__('Condition deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Condition was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
}
4

1 に答える 1

1

habtm データを含めるには、 $recursive = 1; が必要です。あなたはそれを0に設定しました

于 2012-08-17T18:18:08.153 に答える