doujinai2183 2009-06-04 15:43
浏览 33

如何在CakePHP中找到HABTM相关记录('list')下拉列表?

I want to generate a list (dropdown) with habtm relationship. For example I have a table called "users" another called "vehicles" and "users_vehicles".

What I want is to get vehicles assigned to "x" user and put it in a drop-down. How can I achieve this?


class User extends AppModel {

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

  • 写回答

2条回答 默认 最新

  • drrkgbm6851 2009-06-04 18:08
    关注

    This is one way, there a probably several others.

    $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');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算