How to select randomly with doctrine

前端 未结 13 1022
耶瑟儿~
耶瑟儿~ 2020-11-27 20:21

Here is how I query my database for some words

$query = $qb->select(\'w\')
    ->from(\'DbEntities\\Entity\\Word\', \'w\')
    ->where(\'w.indiction         


        
13条回答
  •  Happy的楠姐
    2020-11-27 20:31

    In line with what Hassan Magdy Saad suggested, you can use the popular DoctrineExtensions library:

    See mysql implementation here: https://github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/Rand.php

    # config.yml
    
    doctrine:
         orm:
             dql:
                 numeric_functions:
                     rand: DoctrineExtensions\Query\Mysql\Rand
    

    Tested in Doctrine ORM 2.6.x-dev, you can then actually do:

    ->orderBy('RAND()')
    

提交回复
热议问题