【问题标题】:Symfony 2 - fetch the last inserted row from tableSymfony 2 - 从表中获取最后插入的行
【发布时间】:2018-06-12 19:56:18
【问题描述】:

如何重写此代码以从表中获取最后插入的记录?

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

我尝试了类似的东西

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

但这对我不起作用。

【问题讨论】:

    标签: php symfony symfony-2.8


    【解决方案1】:

    可以通过获取插入对象的id来收集

    $em->persist($entity);
    $em->flush();
    $entity->getId();
    

    $entitymanager->getRepository("entity")->findBy([],["id"=>desc])->getId();
    

    【讨论】:

      【解决方案2】:

      您可以将这些函数添加到您的存储库中:

      public function getLastRow(): ?YourEntity
      {
          return $this->findOneBy([], ['id' => 'DESC']);
      }
      
      public function getLastId(): int
      {
          $lastRow = $this->getLastRow();
          return $lastRow ? $lastRow->getId() : 0;
      }
      

      【讨论】:

        【解决方案3】:

        在找了一个之后,我决定自己尝试一下,我认为它不那么冗长:

        $myRepository->findOneBy([], ['id' => 'DESC']);
        

        【讨论】:

          【解决方案4】:

          findBy() 可以通过 order by、limit 和 offset 参数获取最新记录

          $results = $repository->findBy(array(),array('id'=>'DESC'),1,0);
          
          • 第一个参数用于过滤条件
          • 第二个参数按条件排序
          • 第三个参数是限制
          • 第四个参数设置偏移量

          请注意,它会将结果集作为对象数组返回给您,因此您可以从结果中获取单个对象$results[0]

          FindBy() Examples

          【讨论】:

          • 我使用了您的代码并收到此错误:“[Doctrine\ORM\ORMException] 为 Backend\AdminBundle\Entity\MyTable#0 指定的方向顺序无效”
          • @Jacek717 我的错是键/值对 'id'=>'DESC' 检查更新的答案
          • 我认为这行得通!我没有错误。但我仍然有从 $results 数组中获取数据的问题。我的意思是当我尝试输入类似 $output->writeln($results[0]);我收到错误:“PHP 通知:未定义的偏移量:0”。我是 PHP/Symfony 的一个小新手,所以也许这是一个非常愚蠢的问题,但我不知道如何从这个变量中获取信息。你能帮忙吗?
          • @Jacek717 如果你dump$results,你会看到什么
          • 好的,我发现问题出在哪里。我将 limit 的参数从 0 更改为 1,现在一切正常。非常感谢您的帮助!
          【解决方案5】:

          您也可以创建一个存储库方法并在必要时调用它,而不是在您想使用它的地方修改代码。

          /**
           * Repository method for finding the newest inserted
           * entry inside the database. Will return the latest
           * entry when one is existent, otherwise will return
           * null.
           *
           * @return MyTable|null
           */
          public function findLastInserted()
          {
              return $this
                  ->createQueryBuilder("e")
                  ->orderBy("id", "DESC")
                  ->setMaxResults(1)
                  ->getQuery()
                  ->getOneOrNullResult();
          }
          

          参考资料: https://symfony.com/doc/current/doctrine.html#querying-for-objects-the-repository

          【讨论】:

          • 似乎有必要将id 替换为e.id
          【解决方案6】:

          请尝试以下方法

          $repository = $entityManager->getRepository('AdminBundle:MyTable');
          $repository->setMaxResults(1)->orderBy('id', 'DESC');
          $results = $repository->getQuery()->getSingleResult();
          

          参考: https://undebugable.wordpress.com/2016/01/27/symfony2-querybuilder-find-first-and-find-last-record-in-table/

          【讨论】:

          • 我使用了您的方法并收到此错误:“未定义的方法 'orderBy'。该方法必须以 findBy 或 findOneBy 开头!”
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-01-21
          • 2017-12-18
          • 1970-01-01
          • 2019-11-21
          • 2017-10-14
          • 1970-01-01
          • 2014-09-10
          相关资源
          最近更新 更多