How to select randomly with doctrine

前端 未结 13 1011
耶瑟儿~
耶瑟儿~ 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条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 20:49

    I hope this would help others:

            $limit = $editForm->get('numberOfQuestions')->getData();
            $sql = "Select * from question order by RAND() limit $limit";
    
            $statement = $em->getConnection()->prepare($sql);
            $statement->execute();
            $questions = $statement->fetchAll();
    

    Note here the table question is an AppBundle:Question Entity. Change the details accordingly. The number of questions is taken from the edit form, make sure to check the variable for the form builder and use accordingly.

提交回复
热议问题