帮助需要将MySQL查询转换为Doctrine Query

时间:2011-06-06 14:30:56

标签: zend-framework doctrine

我正在尝试转换以下MySQL查询,该查询将所有未预约六个月或更长时间的客户转换为DQL查询。

mysql> select appt.lastDate, clients.firstname, clients.lastname
-> from (select max(gapmtDate) as lastDate,gapmtClient from groomappointments group by gapmtClient) as appt ,clients
-> where date_sub(CURDATE(), INTERVAL 6 MONTH)>lastDate
-> AND clients.clientid = appt.gapmtClient;

我试图将此与以下内容相匹配,但我收到一条错误,指出where子句中有一个未知的列g__0。任何人都可以指出我出错的地方

public function noappointmentsforsixmonthsAction()
{



  $sixmonths=date('y-m-d',strtotime("-6 months"));

  $q=Doctrine_Query::create()
  ->select('MAX(g.gapmtDate) AS lastDate, c.firstname, c.lastname,g.gapmtClient')
 ->from('PetManager_Model_Groomappointments g')
 ->leftJoin('g.PetManager_Model_Clients c')
 ->where('lastDate <?',$sixmonths)
 ->groupBy('g.gapmtClient');

 .......

}

修改

好吧,似乎Doctrine网站整天都在关闭,我已经尝试将查询编辑到下面,但它仍然无效。任何人都可以给我正确的语法。

$q=Doctrine_Query::create()
->select('a.lastDate, c.firstname, c.lastname')
->addSelect('(SELECT MAX(gapmtDate) AS lastDate, gapmtClient FROM PetManager_Model_Groomappointments GROUP BY gapmtClient) as a')
->from('PetManager_Model_Groomappointments a')
 ->leftJoin('a.PetManager_Model_Clients c')   
 ->where('a.gapmtClient=c.clientID')
 ->andWhere('lastDate >?',$sixmonths);

1 个答案:

答案 0 :(得分:0)

试试这个:

$q=Doctrine_Query::create()
  ->select('MAX(g.gapmtDate) AS lastDate, c.firstname, c.lastname, g.gapmtClient')
 ->from('PetManager_Model_Groomappointments g')
 ->leftJoin('g.PetManager_Model_Clients c')
 ->where('MAX(g.gapmtDate) < ?', $sixmonths)
 ->groupBy('g.gapmtClient');

我希望这有效。