加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – symfony2 doctrine expr子查询:错误:参数号无效

发布时间:2020-12-13 17:16:00 所属栏目:PHP教程 来源:网络整理
导读:试图让用户获得喜欢的状态. public function getLikedStatuses(User $user){ $qb = $this-_em-createQueryBuilder(); $qb -select('s.id') -from('WallBundle:Likes','l') -innerJoin('l.status','s') -where('l.user = :user') -setParameter('user',$user)
试图让用户获得喜欢的状态.

public function getLikedStatuses(User $user)
{
    $qb = $this->_em->createQueryBuilder();
                $qb
                ->select('s.id')
                ->from('WallBundle:Likes','l')
                ->innerJoin('l.status','s')
                ->where('l.user = :user')
                ->setParameter('user',$user)
                ->orderBy('s.id','DESC')
            ;

    $qb2=  $this->_em->createQueryBuilder()
        ->select('st')
        ->from('WallBundle:Status','st');

       $qb2 ->andWhere($qb2->expr()->in('st.id',$qb->getDQL()));


    return $qb2->getQuery()->getResult();
}

Error:
Invalid parameter number: number of bound variables does not match number of tokens

BTW:当我转储$qb-> getDQL()时:

string 'SELECT s.id FROM TBWBundleEntityLikes l LEFT JOIN l.status s WHERE l.user = :user' (length=87)

BTW2:当我为'(12073)(状态的id)替换’$qb-> getDQL()’时,它的工作原理……

解决方法

实际上,您可以执行更简单的查询,具体取决于您的注释方式.

就像是 :

$qb =  $this->_em->createQueryBuilder()
    ->select('s')
    ->from('WallBundle:Status','st')
    ->innerJoin('st.like','l')
    ->where('l.user = :user')
    ->setParameter('user',$user)
    ->getQuery()
    ->getResult();

这应该做同样的事情,更短,更容易理解,因为只有一个查询.

更新:我遇到了与今天完全相同的问题,并通过将两个setParameters放在第二个查询中来解决它.因此我找到了解决它的另一种方法!

我做了类似的事情:

$qb = $this->_em->createQueryBuilder()
    ->select('s.id')
    ->from('WallBundle:Likes','l')
    ->innerJoin('l.status','s')
    ->where('l.user = :user')
    ->orderBy('s.id','DESC')
    ->getDQL()
;

$qb2=  $this->_em->createQueryBuilder()
    ->select('st')
    ->from('WallBundle:Status','st');
    ->where('st.like IN('.$qb.')')
    ->setParameter('user',$user)
    ->getQuery()
;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读