Summarize the method of query in Symfony

Source: Internet
Author: User
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 (); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.