今天,我问过/回答了以下问题: https://stackoverflow.com/a/43069362/4521733
现在我尝试使用Doctrine Query Builder创建此查询:
$qb->select('sp')
->addSelect(
'(SELECT COUNT(spa.id) FROM AppBundle:ProjectAttribute spa WHERE spa.project = sp.id AND spa.attribute = pa.attribute AND spa.attributeValue = pa.attributeValue) AS attributeSimilarity'
)
->addSelect(
'(attributeSimilarity * 1) AS similarity'
)
->from('AppBundle:Project', 'p')
->leftJoin('AppBundle:ProjectAttribute', 'pa', 'WITH', 'p = pa.project')
->leftJoin('AppBundle:Project', 'sp', 'WITH', 'p != sp')
->where('p = :project')
->setParameter(':project', $project)
->groupBy('sp.id')
->orderBy('similarity', 'DESC')
->getQuery()
->getResult();
不幸的是,这不起作用:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'attributeSimilarity' in 'field list'
问题是Doctrine重命名了subquerys" AS属性相似性"依此类推到" AS sclr_35",但没有重命名最后一个addSelect中的字段名称。
我已经尝试在一个addSelect中编写所有子查询。但这根本不起作用。
我错了什么?