未定义的偏移:0.学说和本地SQL



我几乎没有问题,我想打印用户的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'];

有人知道怎么了?

  1. EntityResultFieldResult添加到$rsm

    $rsm = new ResultSetMapping();
    $rsm->addEntityResult('AppUserBundleEntityUser', 'u');
    $rsm->addFieldResult('u', 'id', 'id');
    
  2. 更改setParameter

    $ user to $ user-> getUsernameCanonical();

  3. getResult

    foreach($users as $user)
    {
        echo $user->getId();
    }
    

更改字符串:

$query->setParameter(1, $user);

to

$query->setParameter(1, $user->getUsernameCanonical());

相关内容

  • 没有找到相关文章

最新更新