CakePHP:在一个下拉列表中显示多个字段

时间:2012-08-06 05:29:13

标签: cakephp drop-down-menu foreign-keys

$this->loadModel('Product');
$this->set('products',$this->Product->find('list',array('product'=>array('products.name' => 'products.price'))));
$this->set(compact('products'));

我正在尝试这样做,所以我可以在我正在使用的功能中显示产品名称和它的价格。因此,当选择下拉菜单时,而不仅仅是“productname”,它就像“productname - $ price”。

我很确定这是需要看到的所有代码。

1 个答案:

答案 0 :(得分:6)

Product Model中简单添加以下行:

public $virtualFields = array('name_price' => 'concat(Product.name, "-", Product.price)');

并尝试以下代码来获取:

$this->loadModel('Product');
$this->set('products',$this->Product->find('list',array('fields'=>array('Product.name_price' => 'Product.price'))));
$this->set(compact('products'));
相关问题