Symfony - ошибка из-за надстрочных знаков в DQL-запросе - PullRequest
1 голос
/ 18 января 2012

Я делаю:

    $text = '%'.addslashes($text).'%';


    $images = $this->getDoctrine()->getEntityManager()
        ->createQuery("SELECT img, cat, u
                       FROM AcmeMainBundle:Image img
                       JOIN img.category cat
                       JOIN img.user u
                       WHERE img.title LIKE '$text' OR img.description LIKE '$text'
                       ORDER BY img.id DESC")
        ->getResult();  

, и когда $ text содержит некоторые ', он выдает ошибку

[Синтаксическая ошибка] строка 0, столбец 150: Ошибка: ожидается конецстрока, получившая 'T' 500 Внутренняя ошибка сервера - QueryException

Как это исправить?

1 Ответ

1 голос
/ 18 января 2012
   $images = $this->getDoctrine()->getEntityManager()
        ->createQuery("SELECT img, cat, u
                       FROM AcmeMainBundle:Image img
                       JOIN img.category cat
                       JOIN img.user u
                       WHERE img.title LIKE :text OR img.description LIKE :text
                       ORDER BY img.id DESC")
        ->setParameter('text', $text)
        ->getResult();

Или попробуйте это:

   $text = "%".$text."%";
   $images = $this->getDoctrine()->getEntityManager()
        ->createQueryBuilder()
        ->select(array('img','cat','u'))
        ->from('AcmeMainBundle:Image', 'img')
        ->innerJoin('img.category', 'cat')
        ->innerJoin('img.user', 'u')
        ->where('img.title LIKE :title OR img.description LIKE :description')
        ->orderBy('img.id','DESC')
        ->setParameter('title', $title)
        ->setParameter('description', $title)
        ->getResult();
...