douchun2158 2018-01-03 06:34
浏览 79
已采纳

Symfony 2 - 从表中获取最后一个插入的行

How can I rewrite this code in order to get last inserted record from the table?

$repository = $entityManager->getRepository('AdminBundle:MyTable');
$product = $repository->find($id);

I tried something like

$repository->findBy(array('id','DESC')->setMaxResults(1);

But it did not work for me.

  • 写回答

3条回答 默认 最新

  • dqsvf28682 2018-01-03 06:48
    关注

    You could get the latest record by using findBy() with order by and limit paramters

    $results = $repository->findBy(array(),array('id'=>'DESC'),0,1);
    
    • First argument is for filter criteria
    • Second argument takes order by criteria
    • Third argument is for limit
    • Fourth argument sets offset

    Note it will return you the results set as array of objects so you can get single object from result as $results[0]

    FindBy() Examples

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

报告相同问题?

悬赏问题

  • ¥60 如何批量获取json的url
  • ¥15 对法兰连接元件所承受的表面载荷等效转化为法兰开孔接触面上的等效表面载荷?
  • ¥15 comsol仿真压阻传感器
  • ¥15 Python线性规划函数optimize.linprog求解为整数
  • ¥15 llama3中文版微调
  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼