0

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!

Ankus
  • 11
  • 3
  • maybe passing new date as a parameter makes it fail? try to give format to the date time instead of passing the object – Daniel Ramos Apr 08 '17 at 21:37
  • so I modified the code like this but the error still persists on my 1 post entry route and on my latest posts page it doesnt return any posts : $date = new \DateTime(); $date_formatted = $date->format('d.m.Y, H:m'); – Ankus Apr 08 '17 at 21:49
  • What doctrine type is the attribute publishedDate? This line looks wrong aswell: ->setParameter('slug', array()); shouldn't it be ->setParameter('slug', $slug); ? – Daniel Ramos Apr 08 '17 at 21:56
  • the error started with $slug as parameter, i tried to debug and left array() by accident, changed it back to $slug and the error persists, i clean the cache after each change – Ankus Apr 08 '17 at 21:59
  • This is doctrine type for publishedDate : /** * @ORM\Column(name="published_date", type="datetime", nullable=true) */ private $publishedDate; – Ankus Apr 08 '17 at 22:01
  • it looks like everything is alright. :( what does slug contains when you do var_dump? – Daniel Ramos Apr 08 '17 at 22:04
  • It gives : string(9) "angular-2" – Ankus Apr 08 '17 at 22:10

0 Answers0