This article mainly introduced the Symfony query method, combined with the example form summarizes and analyzes the CreateQuery and getquery query data specific use skills, needs friends can refer to the next
This article describes the Symfony query method. Share to everyone for your reference, as follows:
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 ();