2012-05-02 66 views
0

我有這樣的代碼在我products_controller.php文件:如何從cakephp中的may表中選擇並顯示特定字段?

<?php 
    class ProductsController extends AppController{ 
     var $name = 'Products'; 
     var $helpers = array('Form'); 
     //var $scaffold; 

     function index(){ 
      $this->Product->recursive = 1; 
      $products = $this->Product->find('all'); 
      $this->set('products',$products); 
      //pr($products); 
     } 

     function add(){ 
      if(!empty($this->data)){ 
       $this->Product->create(); 
       $this->Product->save($this->data); 
       $this->redirect(array('action'=>'index')); 
      } 


      $categories = $this->Product->Category->find('list',array(
       'field'=>array('Category.categoryName') 
      )); 

      $this->set('categories',$categories); 

      pr($categories); 
     } 
    } 
?> 

什麼它實際上在我的數據庫是這樣的:

SELECT `Category`.`id` FROM `categories` AS `Category` WHERE 1 = 1 

的事情是,我不是要選擇Category.id。我要選擇Category.categoryName這是我的數據庫中的另一個字段,以便它會自動在我add.ctp文件,該文件是這樣的填充一個下拉列表:

<h2>ADD</h2> 

<?php echo $form->create('Product'); ?> 
<?php 
    echo $form->input('ProductName'); 
    echo $form->input('categories'); 
    echo $form->end('DONE'); 
?> 

任何幫助,將不勝感激。

回答

0

在您的類別模型中,使用類別名稱設置displayField Name屬性。

public $displayField = 'categoryName';

+0

哇!像魔術一樣工作。我目前正在遵循本教程: http://www.developmentbd.com/blog/CakePHP-Application-Development.pdf 我不知道,它沒有提到如何操縱顯示字段,特別是在第128頁。謝謝很多gvLearner。 :) – Charmie

+0

那麼現在的問題是它不會保存>。<無論如何,我會盡力解決這個問題。非常感謝。 – Charmie

相關問題