user4793675
user4793675

Reputation:

QueryBuilder query array values

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?

Upvotes: 3

Views: 3218

Answers (1)

Matteo
Matteo

Reputation: 39380

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

Upvotes: 6

Related Questions