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

时间:2013-04-08 10:52:34

标签: php symfony doctrine subquery

试图让用户获得喜欢的状态。

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();
}
  

错误:   参数号无效:绑定变量数与令牌数不匹配

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

string 'SELECT s.id FROM TB\WBundle\Entity\Likes l LEFT JOIN l.status s WHERE l.user = :user' (length=87)

BTW2:当我将($ qb-> getDQL()'替换为(12073)(状态ID)时,它可以正常工作......

4 个答案:

答案 0 :(得分:4)

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

类似的东西:

$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()
;

答案 1 :(得分:0)

尝试为此->where('l.user = :user')替换where('l.user = ?1') 并添加$qb->setParameter(1, $yourValue);

答案 2 :(得分:0)

尝试更改此

$qb2=  $this->_em->createQueryBuilder()
        ->select('st')
        ->from('WallBundle:Status','st');
$qb2 ->andWhere($qb2->expr()->in('st.id',$qb->getDQL()));

 $qb2=  $this->_em->createQueryBuilder()
        ->select('st')
        ->from('WallBundle:Status','st');
        ->where($qb2->expr()->in('st.id',$qb->getDQL()));

答案 3 :(得分:0)

由于学说在使用子查询(look at my comment)时不支持限制,一种可能的解决方案是执行两个单独的查询,这些查询不理想但有效。

/**
 * @param User $user
 * @param int $limit
 * @param int $offset
 * @return User[]
 */
public function getUserFollowers(User $user, $limit = 20, $offset = 0)
{
    $_followersIds = $this->getEntityManager()
        ->createQueryBuilder()
        ->select('IDENTITY(r.followeduser)')
        ->from($this->getEntityName(), 'r')
        ->where('r.followeeuser = :user')
        ->andWhere('r.followeduser !=:user')
        ->setParameter('user', $user)
        ->orderBy('r.id', 'DESC')
        ->setFirstResult($offset)
        ->setMaxResults($limit)
        ->getQuery()
        ->getResult();

    $_usersQb = $this->getEntityManager()->createQueryBuilder();
    $_usersQb
        ->select('u')
        ->from('UserBundle:User', 'u')
        ->where('u.id IN (:followersIds)')
        ->setParameter('followersIds', array_values($_followersIds));

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