代码之家  ›  专栏  ›  技术社区  ›  Elorfin

一个操作中表上的两个请求有问题

  •  2
  • Elorfin  · 技术社区  · 14 年前

    我有一个操作,在一个唯一的表上有两个请求。请求的结果必须不同。

    但是,对于我的两个请求,结果是相同的,它来自于我的第二个请求。

        // Récupération du (ou des) locataire(s) actuel(s) du logement
        $this->locataires = Doctrine_Query::create()
          ->from('logement l')
          ->leftJoin('l.Bail b')
          ->leftJoin('b.Locataire')
          ->where('l.id = ?', $request->getParameter('id'))
          ->andWhere('(b.datefin >= ?', date('Y-m-d', time()))
          ->orWhere("b.datefin = '0000-00-00')")
          ->execute();
    
        // Récupération du (ou des) locataire(s) précédent(s) du logement
        $this->locatairesprec = Doctrine_Query::create()
          ->from('logement l')
          ->leftJoin('l.Bail b')
          ->leftJoin('b.Locataire')
          ->where('l.id = ?', $request->getParameter('id'))
          ->andWhere('b.datefin < ?', date('Y-m-d', time()))
          ->andWhere("b.datefin != '0000-00-00'")
          ->orderBy('datedeb')
          ->execute();
    
    2 回复  |  直到 14 年前
        1
  •  0
  •   Rene Terstegen    14 年前

    比较它生成的两个查询。我不知道你怎么能在教义1中做到这一点。在条令2中,您可以启用一个记录器,因此所有执行的SQL都将被写入,例如stdout。

    还有一件事。

    当您在查询中使用当前时间戳时,您应该用当前时间戳定义一个变量,并在两个查询中使用此变量。在这种情况下,它将类似于:

    $currentTime = time();
    
    // Récupération du (ou des) locataire(s) actuel(s) du logement
    $this->locataires = Doctrine_Query::create()
      ->from('logement l')
      ->leftJoin('l.Bail b')
      ->leftJoin('b.Locataire')
      ->where('l.id = ?', $request->getParameter('id'))
      ->andWhere('(b.datefin >= ?', $currentTime)
      ->orWhere("b.datefin = '0000-00-00')")
      ->execute();
    
    // Récupération du (ou des) locataire(s) précédent(s) du logement
    $this->locatairesprec = Doctrine_Query::create()
      ->from('logement l')
      ->leftJoin('l.Bail b')
      ->leftJoin('b.Locataire')
      ->where('l.id = ?', $request->getParameter('id'))
      ->andWhere('b.datefin < ?', $currentTime)
      ->andWhere("b.datefin != '0000-00-00'")
      ->orderBy('datedeb')
      ->execute();
    

    两条语句的执行之间可能会延迟(超过)秒,这会使查询不可靠。

        2
  •  0
  •   Elorfin    14 年前

    这可能不是最好的方法,但我已经解决了在一个请求中选择表中的所有行的问题,之后,我使用PHP对结果进行排序。