问题
trying to get liked statuses by 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: when i dump the $qb->getDQL():
string 'SELECT s.id FROM TB\WBundle\Entity\Likes l LEFT JOIN l.status s WHERE l.user = :user' (length=87)
BTW2: when i replace the '$qb->getDQL()' for (12073) (id of status) it works...
回答1:
Actually you can probably do a simpler query, depending on how you did your annotations.
Something like :
$qb = $this->_em->createQueryBuilder()
->select('s')
->from('WallBundle:Status','st')
->innerJoin('st.like','l')
->where('l.user = :user')
->setParameter('user', $user)
->getQuery()
->getResult();
This should do the same, is shorter and is easier to understand as there is only one query.
Update : I have had the exact same problem as you did today and resolved it by putting the two setParameters
in the second query. And therefore I found another way to solve it!
I did something exactly like that :
$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()
;
回答2:
Try to replace ->where('l.user = :user')
for this where('l.user = ?1')
and add $qb->setParameter(1, $yourValue);
回答3:
Try to change this
$qb2= $this->_em->createQueryBuilder()
->select('st')
->from('WallBundle:Status','st');
$qb2 ->andWhere($qb2->expr()->in('st.id',$qb->getDQL()));
to
$qb2= $this->_em->createQueryBuilder()
->select('st')
->from('WallBundle:Status','st');
->where($qb2->expr()->in('st.id',$qb->getDQL()));
回答4:
As doctrine doesn't support a limit when using subquery (look at my comment) one possible solution is to execute two separate queries which is not ideal but works.
/**
* @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();
}
来源:https://stackoverflow.com/questions/15877287/symfony2-doctrine-expr-subquery-error-invalid-parameter-number