phpsymfonydatetimepostsfindby

Findby DateTime the recent uploaded articles Symfony


Hello im triying to get on my dashboard only the most recent articles but isnt working i did this function

public function PostsRecientes(){
    return $this->getEntityManager()
    ->createQuery('        
    SELECT post
    FROM App:Posts post 
    WHERE post.fechaPublicacion > CURRENT_DATE()')
    ->getResult();
}

and this in my dashboardcontroller

/**
 * @Route("/", name="dashboard")
 */
public function index(PaginatorInterface $paginator, Request $request): Response
{
    
    $em = $this->getDoctrine()->getManager();
    $posts = $em->getRepository(Posts::class)->PostsRecientes();
    return $this->render('dashboard/index.html.twig', [
        'posts' => $posts
    ]);
}

but when i do a var dump i doesnt bring any post.


Solution

  • You can use setFirstResult and setMaxResults, for example:

    public function PostsRecientes()
    {
        $qb = $this->createQueryBuilder('p')
            ->orderBy('p.id', 'DESC')
            ->setFirstResult(0)
            ->setMaxResults(6);
    
        return $qb->getQuery()->getResult();
    }