php - Get Records from database of all months -
i developing web application in symfony. want fetch records database according months. query is:
$totalsearchesbyadminall = $em->createquerybuilder() ->select('count(searchhistory.id) totalsearchesbyadmin') ->from('drpadminbundle:log', 'searchhistory') ->where('searchhistory.last_updated :last_updated') ->setparameter('last_updated',??????.'%') ->andwhere('searchhistory.event = :event') ->setparameter('event','admin_search') ->getquery() ->getarrayresult();
last_updated datetime field , stores in database like: 2015-06-12 11:50:44
i want records jan=>5 feb=>10 .....and on..
please help
you can group data month , year date_format
mysql statement.
in order use mysql function in dql doctrine statement suggest install mapado/mysql-doctrine-functions doctrine function. add composer.json , enable in config.yml follow:
#app/config/config.yml orm: auto_generate_proxy_classes: %kernel.debug% entity_managers: default: mappings: .... dql: datetime_functions: date_format: mapado\mysqldoctrinefunctions\dql\mysqldateformat
then can use in repository example:
$qb = $this->createquerybuilder('p') ->select("distinct date_format(p. last_updated,'%m-%y') formatted_date") ->andwhere('p.searchhistory.event = :event') ->setparameter('event','admin_search') ->addgroupby("formatted_date") ->orderby('p.last_updated') ->getquery() ->getresult();
you need modify example add count
hope help.
Comments
Post a Comment