按字符串的长度排序

时间:2012-08-26 12:03:02

标签: doctrine-orm

有没有办法按 Title 字段中包含的字符串长度订购此查询?

    $query = $this->getEntityManager()
        ->createQuery('
    SELECT b FROM AcmeTestBundle:Blog b
    WHERE b.Title LIKE :title'
    )->setParameter('title','%'.$title.'%');

ORDER BY b.Title.length()似乎不起作用。

1 个答案:

答案 0 :(得分:5)

是的,试试吧:

$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.'%');