2009-06-04 6 views
1

habtm関係でリスト(ドロップダウン)を生成したい。たとえば、私は "車両"と "users_vehicles"と呼ばれる別の "ユーザー"と呼ばれるテーブルを持っています。CakePHPのドロップダウンに関するHABTM関連レコードの(リスト)を見つけるにはどうすればよいですか?

私が望むのは、 "x"ユーザーに割り当てられた車をドロップダウンに入れることです。どうすればこれを達成できますか?

 

class User extends AppModel { 

    var $hasAndBelongsToMany = array('Vehicle'); 
} 

答えて

3

これは1つの方法ですが、おそらくいくつかあります。

$result = $this->User->Vehicle->find('all', array(
    'recursive' => -1, 
    'conditions' => array('Ownership.user_id' => 66), 
    'fields' => array('Vehicle.*','Ownership.*'), 
    'joins' => array(
     array(
      'table' => 'users_vehicles', 
      'alias' => 'Ownership', 
      'type' => 'LEFT', 
      'foreignKey' => false, 
      'conditions'=> 'Vehicle.id = Ownership.vehicle_id' 
     ) 
    ) 
)); 
$list = Set::combine($result,'{n}.Vehicle.id','{n}.Vehicle.name'); 
1
<code> 
$options['conditions']['Ownership.vehicle_id'] = $x; 
$options['joins'] = array(
          array(
            'table' => 'ownership', 
              'alias' => 'Ownership', 
              'type' => 'Left', 
              'conditions' => 'Vehicle.id = Ownership.vehicle_id' 
              ) 
            ); 
$this->set('vehicles', $this->Vehicle->find('list', $options)); 
</code> 
+0

は私のために働きました。どうも! – fabianpimminger

関連する問題