我的 products_controller.php 中有这段代码:
<?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');
?>
任何帮助将不胜感激。