dssqq82402 2013-08-13 04:17
浏览 88
已采纳

使用模型内的query()方法从表中获取单个值

I have a very complex setup on my tables and achieving this via any of the find() methods is not an option for me, since I would need to fix relationships between my tables and I don't have the time right now, so I'm looking for a simple fix here.

All I want to achieve is run a query like this:

SELECT MAX( id ) as max FROM MyTable WHERE another_field_id = $another_field_id

Then, I need to assign that single id to a variable for later use.

The way I have it now it returns something like [{{max: 16}}], I'm aware I may be able to do some PHP on this result set to get the single value I need, but I was hoping there was already a way to do this on CakePHP.

  • 写回答

2条回答 默认 最新

  • dongpang9573 2013-08-13 06:41
    关注

    Assuming you have a model for your table and your are using CakePHP 2.x, do:

    $result = $this->MyTable->field('id', array('1=1'), 'id DESC');
    

    This will return a single value.

    see Model::field()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程