2012-08-17 69 views
0

我有一個簡單的cakephp 2.x應用程序,使用烘焙腳本烘焙。兩張主表是條件和藥物。一種情況HABTM藥物和副verca。我試圖在條件指數中列出屬於條件的藥物,基本列出所有條件,但僅限於那些藥物。我曾嘗試做這樣,顯示屬於在一個逗號分隔的列表中的條件,這在其他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 /查看/條件/ index.ctp,第22行]和爲foreach()提供的無效參數[APP/View/Conditions/index.ctp,line 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 2012-08-17 16:46:47

+0

第22行在哪裏? – 2012-08-17 17:11:31

+0

@ALS第22行是'foreach($ condition ['Drug'] as $ drug):'@Hoff如果不改變關係,是不可能的?我正在生成所有藥物的藥物複選框列表。所以當添加新的條件時,他們可以檢查相關的藥物。我不確定改變這種關係是否也需要改變這種關係。它看起來像我需要一個查找調用每個條件ID? – Jonathan 2012-08-17 17:51:16

回答

1

要包括的habtm數據,您需要$遞歸= 1; 你已經將它設置爲0

+0

哈哈哈!這太小又簡單。非常感謝你! – Jonathan 2012-08-17 18:26:27