3

I have a query to get a username from the entity with date:

return $this->createQueryBuilder('t')
    ->select('t.username')
    ->where('t.date = :date')
    ->andWhere('t.username = :username')
    ->setParameter('date', $date)
    ->setParameter('username', $username)
    ->getQuery()
;

However, when I want to find a username in the array, I use this:

if (in_array($user_array, $username) != true) {

I get this $username array:

array(1) {
  [0] =>
  array(1) {
    'username' =>
    string(9) "username1"
  }
}
array(1) {
  [0] =>
  array(1) {
    'username' =>
    string(10) "username90"
  }
}
array(1) {
  [0] =>
  array(1) {
    'username' =>
    string(10) "username12"
  }
}

Is it even possible to search if the username is within the array? Do I need to adjust my QueryBuilder code or do I need to resort to another solution in Symfony?

1
  • Adjust your QueryBuilder is cleaner Commented Aug 3, 2015 at 9:37

1 Answer 1

6

you can use the in function. As Example:

    public function getUser($arrays, $date)
    {
 $qb = $this->createQueryBuilder('t');
return $qb
    ->select('t.username')
    ->where('t.date = :date')
    ->andWhere($qb->expr()->in('t.username', $arrays) )
    ->setParameter('date', $date)
    ->getQuery()
;
}

More info here

Hope this help

Sign up to request clarification or add additional context in comments.

2 Comments

That is a good best practice. I now check if the username is not in the array in the function. Thank you for the referenced document.
Hi @Ricardo you are welcome! Of course, exist the expression $qb->expr()->notIn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.