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

dflying 阅读:190 2025-06-02 22:19:02 评论:0

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

我正在努力做到这一点,这样我就可以在我正在使用的功能中同时显示产品名称和价格。因此,当下拉菜单被选中时,它不会只是“产品名称”,而是“产品名称 - $价格”。

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

请您参考如下方法:

只需在您的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')); 


标签:PHP
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号