两个学说请求存在问题

发布于 2024-09-07 06:15:44 字数 5078 浏览 3 评论 0原文

您好,我在请求方面遇到了一个小问题:在一个动作executeFiche中,我有三个请求

public function executeFiche(sfWebRequest $request){

   // Récupération du logement correspondant à l'ID passé dans l'URL
   $this->forward404Unless($this->logement = Doctrine::getTable('Logement')->find(array($request->getParameter('id'))), sprintf('Object logement does not exist (%s).', $request->getParameter('id')));

   // Récupération du (ou des) locataire(s) actuel(s) du logement
   $locataires = Doctrine::getTable('Logement')->createQuery('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
 $locatairesprec = Doctrine::getTable('Logement')->createQuery('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();

 $this->locataires = $locataires;
 $this->locatairesprec = $locatairesprec;
  }

问题是我的两个请求(第一个是好的)阻碍了自己并且返回的结果是错误的。

编辑: SQL 请求

    SELECT l.id AS l__id, l.adresse AS l__adresse, l.montee AS l__montee, l.etage AS
 l__etage, l.numetage AS l__numetage, l.numlogt AS l__numlogt, l.taille AS l__taille,
 l.surfacehab AS l__surfacehab, l.typelog AS l__typelog, l.intergen AS l__intergen,
 l.ascenseur AS l__ascenseur, l.ascenseuracc AS l__ascenseuracc, l.accessibl AS 
l__accessibl, l.adaptable AS l__adaptable, l.adapte AS l__adapte, l.chauffage AS 
l__chauffage, l.chargeschauf AS l__chargeschauf, l.chargeseauch AS l__chargeseauch,
 l.chargeseaufr AS l__chargeseaufr, l.reservataire AS l__reservataire, l.loyer AS 
l__loyer, l.loyercc AS l__loyercc, l.commentaires AS l__commentaires, l.created_at AS 
l__created_at, l.updated_at AS l__updated_at, b.id AS b__id, b.locataire AS b__locataire, 
b.logement AS b__logement, b.datedeb AS b__datedeb, b.datefin AS b__datefin, b.colloc AS 
b__colloc, b.bailglissant AS b__bailglissant, l2.nud AS l2__nud, l2.titre AS l2__titre,
 l2.nom AS l2__nom, l2.prenom AS l2__prenom, l2.nationalite AS l2__nationalite, 
l2.datenaissance AS l2__datenaissance, l2.statutmatri AS l2__statutmatri, l2.statutpro AS
 l2__statutpro, l2.nbenfants AS l2__nbenfants, l2.monoparental AS l2__monoparental, 
l2.numprec AS l2__numprec, l2.rueprec AS l2__rueprec, l2.quartierprec AS l2__quartierprec,
 l2.codepostalprec AS l2__codepostalprec, l2.villeprec AS l2__villeprec, l2.statutlogprec 
AS l2__statutlogprec FROM logement l LEFT JOIN bail b ON l.id = b.logement LEFT JOIN 
locataire l2 ON b.locataire = l2.nud WHERE (l.id = '1' AND (b.datefin >= '2010-07-01' OR
 b.datefin = '0000-00-00'))

    0.03s, "doctrine" connection
    #

    SELECT l.id AS l__id, l.adresse AS l__adresse, l.montee AS l__montee, l.etage AS 
l__etage, l.numetage AS l__numetage, l.numlogt AS l__numlogt, l.taille AS l__taille,
 l.surfacehab AS l__surfacehab, l.typelog AS l__typelog, l.intergen AS l__intergen, 
l.ascenseur AS l__ascenseur, l.ascenseuracc AS l__ascenseuracc, l.accessibl AS 
l__accessibl, l.adaptable AS l__adaptable, l.adapte AS l__adapte, l.chauffage AS 
l__chauffage, l.chargeschauf AS l__chargeschauf, l.chargeseauch AS l__chargeseauch, 
l.chargeseaufr AS l__chargeseaufr, l.reservataire AS l__reservataire, l.loyer AS l__loyer,
 l.loyercc AS l__loyercc, l.commentaires AS l__commentaires, l.created_at AS l__created_at, l.updated_at AS l__updated_at, b.id AS b__id, b.locataire AS b__locataire, 
b.logement AS b__logement, b.datedeb AS b__datedeb, b.datefin AS b__datefin, b.colloc AS
 b__colloc, b.bailglissant AS b__bailglissant, l2.nud AS l2__nud, l2.titre AS l2__titre,
 l2.nom AS l2__nom, l2.prenom AS l2__prenom, l2.nationalite AS l2__nationalite, 
l2.datenaissance AS l2__datenaissance, l2.statutmatri AS l2__statutmatri, l2.statutpro AS 
l2__statutpro, l2.nbenfants AS l2__nbenfants, l2.monoparental AS l2__monoparental, 
l2.numprec AS l2__numprec, l2.rueprec AS l2__rueprec, l2.quartierprec AS l2__quartierprec,
 l2.codepostalprec AS l2__codepostalprec, l2.villeprec AS l2__villeprec, l2.statutlogprec
 AS l2__statutlogprec FROM logement l LEFT JOIN bail b ON l.id = b.logement LEFT JOIN 
locataire l2 ON b.locataire = l2.nud WHERE (l.id = '1' AND b.datefin < '2010-07-01' AND
 b.datefin != '0000-00-00') ORDER BY datedeb

编辑

感谢您的回答,

但是当我想将查询放入我的模型中时,我遇到了其他问题:我有一个错误,带有 '$request ->getParameter('id')'。我将其交换为“$this->getId()”,Doctrine 告诉我有一个错误。

对于括号,我在下面将其关闭。我不知道另一种方法来生成 SQL 的顺序在哪里。它是有:

WHERE l.id = $request->getParameter('id') AND ( b.datefin >= date('Y-m-d', time()) OR b.datefin = 0000-00-00 )

编辑: 我仍然有我的问题。当第二个请求要返回某些内容时,第一个请求不会返回所有条目

Hello I have a little problem with requests : In an action executeFiche, I have three requests

public function executeFiche(sfWebRequest $request){

   // Récupération du logement correspondant à l'ID passé dans l'URL
   $this->forward404Unless($this->logement = Doctrine::getTable('Logement')->find(array($request->getParameter('id'))), sprintf('Object logement does not exist (%s).', $request->getParameter('id')));

   // Récupération du (ou des) locataire(s) actuel(s) du logement
   $locataires = Doctrine::getTable('Logement')->createQuery('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
 $locatairesprec = Doctrine::getTable('Logement')->createQuery('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();

 $this->locataires = $locataires;
 $this->locatairesprec = $locatairesprec;
  }

The problem is my two requests (the first is alright) hinder themselves and the result returned is wrong.

Edit : SQL request

    SELECT l.id AS l__id, l.adresse AS l__adresse, l.montee AS l__montee, l.etage AS
 l__etage, l.numetage AS l__numetage, l.numlogt AS l__numlogt, l.taille AS l__taille,
 l.surfacehab AS l__surfacehab, l.typelog AS l__typelog, l.intergen AS l__intergen,
 l.ascenseur AS l__ascenseur, l.ascenseuracc AS l__ascenseuracc, l.accessibl AS 
l__accessibl, l.adaptable AS l__adaptable, l.adapte AS l__adapte, l.chauffage AS 
l__chauffage, l.chargeschauf AS l__chargeschauf, l.chargeseauch AS l__chargeseauch,
 l.chargeseaufr AS l__chargeseaufr, l.reservataire AS l__reservataire, l.loyer AS 
l__loyer, l.loyercc AS l__loyercc, l.commentaires AS l__commentaires, l.created_at AS 
l__created_at, l.updated_at AS l__updated_at, b.id AS b__id, b.locataire AS b__locataire, 
b.logement AS b__logement, b.datedeb AS b__datedeb, b.datefin AS b__datefin, b.colloc AS 
b__colloc, b.bailglissant AS b__bailglissant, l2.nud AS l2__nud, l2.titre AS l2__titre,
 l2.nom AS l2__nom, l2.prenom AS l2__prenom, l2.nationalite AS l2__nationalite, 
l2.datenaissance AS l2__datenaissance, l2.statutmatri AS l2__statutmatri, l2.statutpro AS
 l2__statutpro, l2.nbenfants AS l2__nbenfants, l2.monoparental AS l2__monoparental, 
l2.numprec AS l2__numprec, l2.rueprec AS l2__rueprec, l2.quartierprec AS l2__quartierprec,
 l2.codepostalprec AS l2__codepostalprec, l2.villeprec AS l2__villeprec, l2.statutlogprec 
AS l2__statutlogprec FROM logement l LEFT JOIN bail b ON l.id = b.logement LEFT JOIN 
locataire l2 ON b.locataire = l2.nud WHERE (l.id = '1' AND (b.datefin >= '2010-07-01' OR
 b.datefin = '0000-00-00'))

    0.03s, "doctrine" connection
    #

    SELECT l.id AS l__id, l.adresse AS l__adresse, l.montee AS l__montee, l.etage AS 
l__etage, l.numetage AS l__numetage, l.numlogt AS l__numlogt, l.taille AS l__taille,
 l.surfacehab AS l__surfacehab, l.typelog AS l__typelog, l.intergen AS l__intergen, 
l.ascenseur AS l__ascenseur, l.ascenseuracc AS l__ascenseuracc, l.accessibl AS 
l__accessibl, l.adaptable AS l__adaptable, l.adapte AS l__adapte, l.chauffage AS 
l__chauffage, l.chargeschauf AS l__chargeschauf, l.chargeseauch AS l__chargeseauch, 
l.chargeseaufr AS l__chargeseaufr, l.reservataire AS l__reservataire, l.loyer AS l__loyer,
 l.loyercc AS l__loyercc, l.commentaires AS l__commentaires, l.created_at AS l__created_at, l.updated_at AS l__updated_at, b.id AS b__id, b.locataire AS b__locataire, 
b.logement AS b__logement, b.datedeb AS b__datedeb, b.datefin AS b__datefin, b.colloc AS
 b__colloc, b.bailglissant AS b__bailglissant, l2.nud AS l2__nud, l2.titre AS l2__titre,
 l2.nom AS l2__nom, l2.prenom AS l2__prenom, l2.nationalite AS l2__nationalite, 
l2.datenaissance AS l2__datenaissance, l2.statutmatri AS l2__statutmatri, l2.statutpro AS 
l2__statutpro, l2.nbenfants AS l2__nbenfants, l2.monoparental AS l2__monoparental, 
l2.numprec AS l2__numprec, l2.rueprec AS l2__rueprec, l2.quartierprec AS l2__quartierprec,
 l2.codepostalprec AS l2__codepostalprec, l2.villeprec AS l2__villeprec, l2.statutlogprec
 AS l2__statutlogprec FROM logement l LEFT JOIN bail b ON l.id = b.logement LEFT JOIN 
locataire l2 ON b.locataire = l2.nud WHERE (l.id = '1' AND b.datefin < '2010-07-01' AND
 b.datefin != '0000-00-00') ORDER BY datedeb

EDIT

Thanks for this answer,

But when I want to put the queries in my model, I have others problems : I have an error, with '$request->getParameter('id')'. I exchange it to '$this->getId()' and Doctrine tell me I have an error.

For the parenthesis, I close them in the next. I don't know another way to generate SQL with order in the where. It is to have :

WHERE l.id = $request->getParameter('id') AND ( b.datefin >= date('Y-m-d', time()) OR b.datefin = 0000-00-00 )

Edit : I still have my problem. When the second request have something to return, the first doesn't return all the entries

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2024-09-14 06:15:44

作为一个好的 symfony 实践,您可以首先将查询放入模型中(例如 LogementTable.class.php)。

您的查询中还存在一些语法问题。


更新: 我没有注意到后面的行上的括号被关闭

在下面的行中,您打开括号,但它没有在之后关闭:

->andWhere('(b.datefin >= ?', date('Y-m-d', time()))

第二个错误,在SQL 日期必须用引号引起来:

->orWhere("b.datefin = '0000-00-00')")
// ...
->andWhere("b.datefin != '0000-00-00'")

更新 2:

尝试将此作为您的第二个请求:

 $locatairesprec = Doctrine::getTable('Logement')->createQuery('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();

As a good symfony practice, you could start by putting the queries in a model (something like LogementTable.class.php).

You have also some syntax problems in your queries.


Update : I didn't notice the parenthesis is closed on the line after

In the following line, you open the parenthesis but it isn't closed after :

->andWhere('(b.datefin >= ?', date('Y-m-d', time()))

Second error, in SQL dates must be surrounded by quotes :

->orWhere("b.datefin = '0000-00-00')")
// ...
->andWhere("b.datefin != '0000-00-00'")

Update 2 :

Try this as your second request :

 $locatairesprec = Doctrine::getTable('Logement')->createQuery('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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文