我有一个实体,如下所示:
private $username;
private $password;
private $createdAt; //datetime
我想在学说中运行查询以获取具有现有字段加上另一个字段的实体(在本例中,该列显示了分钟的createdAt和当前日期时间的差异。
在SQL中,我将运行查询,如:
select username, password, createdAt,TIMESTAMPDIFF(MINUTE , created_at, NOW()) AS minutes from users
在学说中,我按以下方式获取实体:
$user = $em->getRepository('TestBundle:Users');
我如何从学说中获得自定义领域的纪要?
最佳答案
您应该检查ResultSetMapping:
并检查如何添加scalar result
$rsm = new ResultSetMapping;
$rsm->addEntityResult('Users', 'u');
$rsm->addFieldResult('u', 'id', 'id');
$rsm->addFieldResult('u', 'password', 'password');
$rsm->addScalarResult('minutes', 'minutes');
$query = $this->_em->createNativeQuery('select username, password, TIMESTAMPDIFF(MINUTE , created_at, NOW()) AS minutes', $rsm);
$users = $query->getResult();