1

我愿意:

    $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

如何解决?

4

1 回答 1

1
   $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();
于 2012-01-18T09:09:22.753 回答