Skip to content

Instantly share code, notes, and snippets.

@taner-dll
Last active December 16, 2019 03:07
Show Gist options
  • Save taner-dll/9843372 to your computer and use it in GitHub Desktop.
Save taner-dll/9843372 to your computer and use it in GitHub Desktop.
Symfony 2 - Query Builder Order By Rand
$sorular = $em->createQueryBuilder()
->select('s')
->from('PauSemYonetimBundle:Sorular', 's')
->where('s.testler = :test')
->setParameter('test',$test)
->getQuery()
->getResult();
//sorularin dizideki sıralamasını değiştir
shuffle($sorular);
//sıraları değiştirilen soru id lerini yeniden diziye at
$soruIDs = array();foreach($sorular as $s){$soruIDs[] = $s->getId();}
//karışık sıradan 0'dan itibaren n tane soru getir
$output = array_slice($soruIDs, 0, $n);
$qb = $em->createQueryBuilder();
$sorular = $qb->select('s')
->from('PauSemYonetimBundle:Sorular', 's')
->andwhere($qb->expr()->in('s.id', $output))
->getQuery()
->getResult();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment