我正在尝试统计列字段包含特定字符串的记录。
这是我的问题:
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQueryBuilder()
->select("count(e.$column)")
->from("AppBundle:$table", "e")
->where("e.urlid LIKE "-test"")
->getQuery();
$occurences = $query->getSingleScalarResult();
当我在mysql上运行手动查询时,它是有效的,所以它可能是一个教条。我从学说中得到了这个错误:
[语法错误]第0行,第66列:错误:应为StateFieldPathExpression | string | InputParameter | FunctionsReturnStrings | AggregateExpression,得到"
任何帮助都将不胜感激!!
试着用类似的东西来纠正设置类似的表达式:
$qb->andWhere($qb->expr()->like('user.name', $qb->expr()->literal('%'.$name.'%')));
在你的情况下,试试这个:
$key = '-test';
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$query = $qb
->select("count(e.$column)")
->from("AppBundle:$table", "e");
$qb->andWhere($qb->expr()->like('e.urlid', $qb->expr()->literal('%'.$key.'%')));
$occurences = $qb->getQuery()->getSingleScalarResult();
希望这能帮助
我最终设法修复了它lke this:
$query = $this->em->createQueryBuilder()
->select("count(e.$column)")
->from("AppBundle:$table", "e")
->where("e.$column LIKE :url")
->setParameter("url", $url."%")
->getQuery();
$occurences = $query->getSingleScalarResult();