mysql - 将 MySql 查询转换为 CakePhp 2.0

标签 mysql cakephp-2.0

如何将此 MySql 查询转换为 CakePhp 的查找。 请告诉我如何练习在 cakephp 中编写查找查询

select distinct trips.fk_userid from spots, trips 
where spots.fk_tripid = trips.id 
    and trips.isapproved = 1 
    and spots.id in (".$row[$first_index]['spot_list'].")

最佳答案

模型可以是Trip 可以这样查询

$this->Trip->query("select distinct trips.fk_userid from spots, trips where spots.fk_tripid = trips.id and trips.isapproved = 1 and spots.id in (".$row[$first_index]['spot_list'].")");

你应该创建 Trip 和 Spot 模型,在 Trip 模型中,你必须在 Spot 模型中有这个:

public $belongsTo = array(
    'Trip' => array(
        'className'     => 'Trip',
        'foreignKey'    => 'fk_tripid'
    )
);

然后这样查询:

$this->Spot->find('all', array(
   'fields' => array("distinct Trip.fk_userid"), 
   'conditions' => array(
       'Trip.isapproved' => 1, 
       'Spot.id' => $row[$first_index]['spot_list']
)
));

关于mysql - 将 MySql 查询转换为 CakePhp 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28087636/

相关文章:

cakephp - 在自定义组件中使用 session 组件

php - 上传图像不会上传,除非我添加了我不想要的特定代码行

mysql - SQL错误 "is not in the GROUP BY list"

java - 如何将mysql与java连接用于html表单?

cakephp - 单页 cakephp 上具有相同型号名称的多个表单

php - 检查一个模型是否与 cakephp 中的另一个相关联?

php - CakePHP 2.0 说电子邮件无效

mysql - 如何在 mongodb 中表示线程评论(以及评论投票)的数据?

mysql - 如何解决请安装mysql适配器: `gem install activerecord-mysql-adapter` (no such file to load -- java)

mysql - CakePHP 分页器分组依据/排序依据