This article mainly and everybody introduced the Symfony query method, combined with the example form summary analysis CreateQuery and getquery query data specific use skill, need friend can refer to, hope can help everybody.
1. The wording of CreateQuery
$sql = ' SELECT COUNT (DISTINCT (G.GOODSID)) from Appbundle:goodsindex g WHERE g.status =: status '; $params = array (' status ' = = Goodsindex::status_normal,), if (!empty ($keywords)) {$params [' keywords '] = "%{$keywords}%"; $sql. = ' and G.keyword S like:keywords ';} if (!empty ($warehouseIdList)) { $params [' warehouseidlist '] = $warehouseIdList; $sql. = "and G.warehouseid in:(warehouseidlist)"; } $goodsNum = $this->entitymanager->createquery ($sql)->setparameters ($params)->getsinglescalarresult ( );
Personal Summary:: Refers to the placeholder meaning to prevent SQL injection. So all the required parameters are made into an array $params inside.
2. The wording of Getquery
$orderBy = ' P. '. $searchOptions [' by ']; $repository = $this->entitymanager->getrepository (' Appbundle: Goodsindex '); $query = $repository->createquerybuilder (' P '); $query->select (' DISTINCT (P.GOODSID) '); $query- >where (' p.keywords like:keywords ')->setparameter (' keywords ', "%{$searchOptions [' keywords ']}%") Andwhere (' P.status =: status ')->setparameter (' status ', Goodsindex::status_normal)->orderby ($orderBy, $ searchoptions[' order '])->setfirstresult ($pagination [' pageSize '] * ($pagination [' page ']-1))->setmaxresults ( $pagination [' pageSize ']), if (!empty ($searchOptions [' warehouseidlist ')) {$query->andwhere ($query->expr ()- >in (' P.warehouseid ', $searchOptions [' warehouseidlist ']);} $goodsIndexList = $query->getquery ()->getresult ();