Search code examples
phpsymfonydoctrinesymfony-2.7

Order data by datetime Symfony 2


I have an array that contains data. I want to show my data by date (the new data at the head of the list) using the attribute "CreatedAt" which type is datetime. here's my controller:

{
    $em = $this->getDoctrine()->getManager();
    $user= $this->get('security.context')->getToken()->getUser();
    $congesshows = $em->getRepository('appUserBundle:Conges')
        ->findBy(array(),array('CreatedAt'=>'DESC'))
    ;

    $role = $user->getRoles() ;
    if($role[0]== "ROLE_ADMIN") {
        $congess = $em->getRepository('appUserBundle:Conges')
            ->findBy( array('etat' => 'En cours'))
        ;
    }
    else {
        $congess = $em->getRepository('appUserBundle:Conges')
            ->findBy(array('etat' => 'En attente'))
        ;
    }

    return $this->render(
        'appUserBundle:interne:please.html.twig',
        array('conges' => $congess,'congesshows'=>$congesshows)
   );
}

Solution

  • Did you try $congesshows = $em->getRepository('appUserBundle:Conges') ->createQueryBuilder(“c”)->orderBy(“CreatedAt”, 'DESC')->getQuery()->getResult() ; ?