I have an action with two requests on an unique table. The results of the requests must be different.
But, the result is the same for my two requests and it comes from my second request.
// 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.d开发者_如何学JAVAatefin = '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();
Compare the two Queries it generates. I don't know how you can accomplish this in Doctrine 1. In Doctrine 2 you can enable a logger, so all executed SQL will be written to, for example, the stdout.
One other thing.
When you use the current timestamp in a query you shoud define a variable with the current timestamp and use this variable in both queries. In this case it would be something like:
$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();
There may be a delay of (more then) a second between the execution of the two statements, which makes your queries unreliable.
It maybe not the best way, but I have resolve the problem to select all the rows of my table in one request, and after, I sort the results using PHP
精彩评论