我几乎没有问题,我想打印用户的ID,但是我有一个错误:"未定义的offest:0"。这是我的代码:
$rsm = new ResultSetMapping();
$em = $this->getDoctrine()->getManager();
$user= $this->get('security.context')->getToken()->getUser();
$query = $em->createNativeQuery('SELECT id FROM fos_user WHERE username_canonical = ?', $rsm);
$query->setParameter(1, $user);
$users = $query->getResult();
echo $users[0]['id'];
有人知道怎么了?
-
将
EntityResult
和FieldResult
添加到$rsm
$rsm = new ResultSetMapping(); $rsm->addEntityResult('AppUserBundleEntityUser', 'u'); $rsm->addFieldResult('u', 'id', 'id');
-
更改
setParameter
$ user to $ user-> getUsernameCanonical();
-
getResult
foreach($users as $user) { echo $user->getId(); }
更改字符串:
$query->setParameter(1, $user);
to
$query->setParameter(1, $user->getUsernameCanonical());