Doctrine DQL query produces Error: Expected Literal, got end of string

1k Views Asked by At

I am currently trying to build a blog website following a course that uses Symfony 2.5.2. (PHP -v 7.0)

To retrieve a post I am using a following Controller

/**
 * @Route(
 *     "/{slug}",
 *     name = "blog_post"
 * )
 * @Template()
 */
public function postAction($slug)
{

    $PostRepo = $this->getDoctrine()->getRepository('AniaBlogBundle:Post');

    $Post = $PostRepo->getPublishedPost($slug);

    if(null === $Post){
        throw $this->createNotFoundException('Post not found');
    }

    return array(
        'post'=> $Post
    );
}

and here is my getPublishedPost function :

 public function getPublishedPost($slug){
    $qb = $this->getQueryBuilder(array(
            'status' => 'published'
    ));

   $qb->andWhere('p.slug = :slug')
       ->setParameter('slug', $slug);

    return $qb->getQuery()->getOneOrNullResult();
}

and getQueryBuilder function :

public function getQueryBuilder(array $params = array()){

    $qb = $this->createQueryBuilder('p')
                    ->select('p, c, t')
                    ->leftJoin('p.category', 'c')
                    ->leftJoin('p.tags', 't');

    if(!empty($params['status'])){
        if('published' == $params['status']){
            $qb->where('p.publishedDate <= :currDate AND p.publishedDate IS NOT NULL')
                ->setParameter('currDate', new \DateTime());
        }else if('unpublished' == $params['status']) {
            $qb->where('p.publishedDate > :currDate OR p.publishedDate IS NULL')
                    ->setParameter('currDate', new \DateTime());
        }
    }

     if(!empty($params['orderBy'])){
         $orderDir = !empty($params['orderDir']) ? $params['orderDir'] : NULL;
         $qb->orderBy($params['orderBy'], $orderDir);
     }

    if(!empty($params['categorySlug'])){
        $qb->andWhere('c.slug = :categorySlug')
                ->setParameter('categorySlug', $params['categorySlug']);
    }

    if(!empty($params['tagSlug'])){
        $qb->andWhere('t.slug = :tagSlug')
            ->setParameter('tagSlug', $params['tagSlug']);
    }

    if(!empty($params['search'])) {
        $searchParam = '%'.$params['search'].'%';
        $qb->andWhere('p.title LIKE :searchParam OR p.content LIKE :searchParam')
            ->setParameter('searchParam', $searchParam);
    }

    return $qb;
}

}

However i get the 500 error saying : [Syntax Error] line 0, col -1: Error: Expected Literal, got end of string.

Thank you in advance for any suggestions!

0

There are 0 best solutions below