douluchuo0801 2014-01-28 16:40
浏览 16
已采纳

Cakephp,按相关模型字段查找订单

I have a News model that has many Comments. What I need is to find ten News that has new Comments.

At first this task is seems to be eazy, I just need to find last ten Comments ($this->Comment->find('all');) and just display related News, but in case I have 2 comments for the same news I will recieve a duplicated news entry.

So, can I order News by Comments date or something?

*And here is solution. Thanks Dave

$this->Comment->find('all', array(
    'order' => array(
        'Comment.created' => 'DESC'
    ),
    'group' => 'News.id',
));
  • 写回答

1条回答 默认 最新

  • donglin9717 2014-01-28 16:49
    关注

    Use MySQL's "GROUP BY" ('group' option in CakePHP). More details:

    http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀