2012-05-18 37 views
0

我在DB,cardscomments兩個表。我有我的CakePHP應用程序啓動並運行。當卡view.ctp ..相關的評論出現在頁面的底部。CakePHP的預先填充相關記錄的外鍵

我想點擊添加新的評論,但明確將其添加爲當前的卡如預填卡領域,以顯示當前類別。

這是我目前鑑於新評論鏈接:

<?php 

echo $this->Html->link(__('New comment'), array('controller' => 'comments','action' => 'add')); 

?> 

這是我新增評論控制器:

public function add() { 
    if ($this->request->is('post')) { 
     $this->Comment->create(); 
     if ($this->Comment->save($this->request->data)) { 
      $this->Session->setFlash(__('The comment has been saved')); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The comment could not be saved. Please, try again.')); 
     } 
    } 
    $cards = $this->Comment->Card->find('list'); 
    $this->set(compact('cards')); 
} 

我如何拿到卡設置爲當前卡爲新增評論?

我也希望能夠增加新的註釋,但與空白卡..準備好用戶,如果從列表中選擇。

回答

0

您可以將cartId附加到鏈接視圖頁上:

array(..., 'action'=>'add', $cartId) 

然後你可以檢索它作爲這樣的:

public function add($cartId = null) { 
    //... 
    if (isPost) { 
     // 
    } else { 
     $this->request->data['Comment']['cart_id'] = $cartId; 
    } 
    //... 
} 

這樣的cartId將填充上獲取選擇框(POST上要使用的發佈參數去確保形式不記得驗證錯誤的情況下,其公佈值)

+0

輝煌的感謝! – curtisp