php - Symfony2 QueryBuilder return product query in order of ids -


i have list of ids in order:

array(57, 12, 29, 25, 11) 

with symfony`s querybuilder need return these products form database objects, , should returned in specific order.

i need return query, not result.

i trying different approaches:

    public function querysortedproductsincategories($id, $type, $slug, $sort)     {          $qb = $this->createquerybuilder('p');         $qb->addselect(array('p', 'gallery'));         $qb->addselect(array('p', 'media'));         $qb->addselect(array('p', 'image'));         $qb->leftjoin("p." . $type, "c");         $qb->leftjoin('p.gallery', 'gallery');         $qb->leftjoin('gallery.galleryhasmedias', 'media');         $qb->leftjoin('media.media', 'image');         $qb->where("c." . $type. "= :id ");         $qb->andwhere("p.status = 1 ");         $qb->setparameter('id', $id); //        $qb->andwhere($qb->expr()->in('p.id', $sort)); //        $qb->andwhere("p.id in (:sort) "); //        $qb->setparameter('sort', $sort);         return $qb->getquery();     } 

i tried in statement.... returns list need, not in correct order.

now starting think not possible that. stackoverflow last chance of figuring out this...

update:

tried create dql function, errors. first time doing , have no idea whats going on...

class field extends \doctrine\orm\query\ast\functions\functionnode {      /**      * @override      */     public function parse(\doctrine\orm\query\parser $parser) {         $parser->match(lexer::t_identifier);         $parser->match(lexer::t_open_parenthesis);         $this->stringprimary = $parser->stringprimary();         $parser->match(lexer::t_comma);         $this->stringsecondary = $parser->stringprimary();         $parser->match(lexer::t_comma);         $this->stringthird = $parser->stringprimary();         $parser->match(lexer::t_close_parenthesis);     }       /**      * @param \doctrine\orm\query\sqlwalker $sqlwalker      *      * @return string      */     public function getsql(\doctrine\orm\query\sqlwalker $sqlwalker)     {         // todo: implement getsql() method.     }  } 

the query:

        $em = $this->getentitymanager();         $doctrineconfig = $em->getconfiguration();         $doctrineconfig->addcustomstringfunction('field', 'mp\shopbundle\doctrine\field');          $qb = $this->createquerybuilder('p');         $qb->addselect(array('p', 'gallery'));         $qb->addselect(array('p', 'media'));         $qb->addselect(array('p', 'image'));         $qb->addselect(array("p, field(p.id, " . implode(", ", $sort) . ") hidden field"));         $qb->leftjoin("p." . $type, "c");         $qb->leftjoin('p.gallery', 'gallery');         $qb->leftjoin('gallery.galleryhasmedias', 'media');         $qb->leftjoin('media.media', 'image'); //        $qb->where("p.id = :sort ");         $qb->where("c." . $type. "= :id ");         $qb->andwhere($qb->expr()->in('p.id', $sort));         $qb->andwhere("p.status = 1 ");         $qb->setparameter('id', $id); //        $qb->setparameter('sort', $sort);         $qb->orderby('field');         return $qb->getquery(); 

error:

error: expected statefieldpathexpression | string | inputparameter | functionsreturningstrings | aggregateexpression, got '23' 

query:

[1/2] queryexception: select p, p, gallery, p, media, p, image, p, field(p.id, 23, 40, 30, 24, 42, 37, 38, 58, 33, 8, 34, 35, 36, 28, 51, 14, 1) hidden field mp\shopbundle\entity\product p left join p.subcategory c left join p.gallery gallery left join gallery.galleryhasmedias media left join media.media image c.subcategory= :id , p.id in(23, 40, 30, 24, 42, 37, 38, 58, 33, 8, 34, 35, 36, 28, 51, 14, 1) , p.status = 1 order field asc 

with mysql, can create dql function able use field :

see other similar question :

doctrine 2 mysql field function in order by


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -