doctrine2 - Symfony 2 & Doctrine: select count and group by return false results -
i have table this
id | adkey | ip | created_at 1 | ns0392djej | 127.0.0.1 | 2016-04-25 09:00:00 2 | ns0392djej | 127.0.0.1 | 2016-04-25 09:20:00 3 | ui0392djpo | 127.0.0.1 | 2016-04-25 09:30:00
the goal add new row each time click on advertise on website.
my problem when want number of click advertise (the name of advertise adkey column)
in example, should ns0392djej ==> 2 ui0392djpo ==> 1
but following request, count 1
first, global getquerybuilder()
*/ public function getquerybuilder() { $querybuilder = $this->getentitymanager()->createquerybuilder(); $querybuilder ->select('e') ->from($this->getentityclass(), 'e') ->orderby('e.id', 'desc'); return $querybuilder; }
then, extend in child class
public function getquerybuilder() { $qb = parent::getquerybuilder(); $qb->addselect( $qb->expr()->count('e.id') . 'as adcount'); $qb->addgroupby('e.adkey'); }
when analyze request notice doctrine made first request id , use these id count request
select distinct id0 ( select a0_.id id0, a0_.adkey adkey1, a0_.ip ip2, a0_.created_at created_at3, count(a0_.id) sclr4 advertiser_clickcounter a0_ group a0_.adkey order a0_.id desc ) dctrn_result limit 30 offset 0
and
select a0_.id id0, a0_.adkey adkey1, a0_.ip ip2, a0_.created_at created_at3, count(a0_.id) sclr4 advertiser_clickcounter a0_ a0_.id in (?) group a0_.adkey order a0_.id desc parameters: [['5', '4', '2']]
do have advice ? correct way ? knowing fact need querybuilder instance result (or if know way convert query querybuilder instance ? )
thank you
what you're doing there doesn't wrong in itself. it's simple group query though, there's no real need extend classes. may in 1 hit.
this work fine (i made assumptions entities might called obviously):
$yourcount = $qb->select('count(a.adkey) adkeycount, a.adkey') ->from('yourbundle:youradkeyentity', 'a') ->groupby('a.adkey') ->getquery() ->getresult();
there's no point selecting adkey entity 1 of grouped entities anyway.
Comments
Post a Comment