是否有方法根据Title
字段中包含的字符串长度对该查询进行排序?
$query = $this->getEntityManager()
->createQuery('
SELECT b FROM AcmeTestBundle:Blog b
WHERE b.Title LIKE :title'
)->setParameter('title','%'.$title.'%');
ORDER BY b.Title.length()
似乎不起作用。
是的,只需尝试:
$query = $this->getEntityManager()->createQuery('SELECT LENGTH(b.Title) l, b FROM AcmeTestBundle:Blog b WHERE b.Title LIKE :title ORDER BY l')->setParameter('title','%'.$title.'%');