Cakephp 将模型的值放置在选择列表中

Cakephp 将模型的值放置在选择列表中,cakephp,selectlist,Cakephp,Selectlist,我想将模型中的字段放置在选择列表中。 我有这个密码 brand.php和car.php模型 class Brand extends AppModel{ var $name = 'Brand'; var $hasMany = 'Car'; } <?php class Car extends AppModel{ var $name = 'Car'; var $belongsTo = 'Brand'; } ?> 类品牌扩展AppModel{

我想将模型中的字段放置在选择列表中。 我有这个密码

brand.php和car.php模型

 class Brand extends AppModel{ 
  var $name = 'Brand'; 
  var $hasMany = 'Car'; 
 }
 <?php   
 class Car extends AppModel{ 
  var $name = 'Car'; 
  var $belongsTo = 'Brand'; 
 } 
 ?>
类品牌扩展AppModel{
var$name=‘品牌’;
var$hasMany=‘汽车’;
}
控制器

   <?php 
   class CarsController extends AppController{
   var $name = 'Cars';
   function index(){
   $this->set('id', $this->Car->find('all'));
   }
   }
  ?>

这是view index.ctp

<?php   
echo $form->create('Search'); 
foreach($id as $options){ 
     echo $options['Brand']['name']."<br>";  // it works
$values = array(); 
$values[] = $options['Brand']['name']; // doesnt work!

} 
echo $this->Form->select('one', $values); // the values for selects just the last one
echo $form->end('search'); 
?>

那么,如何将Brand.php模型中选项值的字段和id放在选择列表中呢?

find('all')
调用更改为
find('list')
调用,并在品牌模型上调用它

这将获得一个关联数组,其中包含所有
品牌
记录的
id
=>
显示字段
。您可以通过在model
var$displayField='my_field'中指定显示字段来修改with field is显示字段

将您的
find('all')
调用更改为
find('list')
调用并在品牌型号上调用它

这将获得一个关联数组,其中包含所有
品牌
记录的
id
=>
显示字段
。您可以通过在model
var$displayField='my_field'中指定显示字段来修改with field is显示字段

$this->set('brands', $this->Car->Brand->find('list'));
在你看来:

echo $this->Form->input('brand_id');
Cake将自动将$brands视图变量链接到brand_id字段选项列表。

在控制器中:

$this->set('brands', $this->Car->Brand->find('list'));
在你看来:

echo $this->Form->input('brand_id');

Cake将自动将$brands视图变量链接到brand_id字段选项列表。

谢谢…它可以工作//在我的控制器$brands=$this->Car->Brand->find('list')$此->设置('id',$brands)//然后在我的视图中echo$this->Form->select('BrandName',$id);谢谢…它能用//在我的控制器$brands=$this->Car->Brand->find('list')$此->设置('id',$brands)//然后在我的视图中echo$this->Form->select('BrandName',$id);