2012-08-17 6 views
0

나는 간단한 bake 스크립트를 사용하여 구운 cakephp 2.x 응용 프로그램을 가지고 있습니다. 두 가지 주요 테이블은 조건과 약물입니다. 상태 HABTM 마약 및 부인장. 나는 조건 색인에있는 조건에 속하는 약품, 모든 조건을 기재하는 기본 약품을 표시하려하지만, 오직 그 약품 만 표시하려고합니다. 나는 또 다른 1.3 응용 프로그램에서 작동하는 쉼표로 분리 된 목록에서 조건에 속하는 약물 표시하려면 다음과 같이 그 일을 시도 :보기에 HABTM 데이터 표시

<?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; 
?> 

를하지만 다음과 같은 두 가지 오류가 있습니다 : 정의되지 않은 인덱스 : 약물 [ APP/View/Conditions/index.ctp, 22 행] 및 foreach() [APP/뷰/조건/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')); 
    } 
} 
+0

[this] (http://stackoverflow.com/questions/11986746/cakephp-find-all-query-on-multiple-models)와 같은 것이 달성하려는 것입니다. ? – Hoff

+0

22 번 라인은 어디에 있습니까? –

+0

@ALS 22 번 라인은'foreach ($ drug [$ Drug '] from $ drug)입니다.'@Hoff 관계를 변경하지 않고는 할 수 없습니까? 나는 모든 마약에 대한 마약 체크 박스 목록을 작성하고 있습니다. 따라서 새로운 상태가 추가되면 관련 약품을 확인할 수 있습니다. 관계를 변경하면 변경해야하는지 잘 모르겠습니다. 그리고 각 컨디션 아이디를 찾기 위해 호출이 필요할 것 같습니다. – Jonathan

답변

1

가 HABTM 데이터를 포함하려면, 당신이 필요로하는 $ 재귀 = 1; 당신은 그것을 0으로 설정했습니다

+0

하하하! 그것은 작고 단순합니다. 고마워요! – Jonathan