2012-05-09 12 views
0

モデルのフィールドを選択リストに配置したいとします。 私はこのコードを持っている...モデルの値を選択リストに配置

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'; 
} 
?> 

コントローラ

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

これがそう

<?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'); 
?> 

index.ctp図であり、どのようにBrand.phpモデルのオプション値のフィールドとIDを選択リスト??

答えて

1

でそれを指定することで、表示フィールドである:あなたのビューで

$this->set('brands', $this->Car->Brand->find('list')); 

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

ケーキは自動的に$ブランドを表示リンクしますvarをbrand_idフィールドのオプションリストに追加します。

+0

ありがとうございます! //私のコントローラで$ brands = $ this-> Car-> Brand-> find( 'list')); $ this-> set( 'id'、$ brands); //次に、私のビューで$ this-> Form-> select( 'BrandName'、$ id)をエコーし​​ます。 – Leoh

2

find('all')コールをfind('list')コールに変更し、ブランドモデルで呼び出します。

これは、Brandレコードのうち、id =>displayFieldという連想配列を取得します。あなたは、フィールドで変更することができるモデルお使いのコントローラでvar $displayField = 'my_field';

関連する問題