doy51723 2015-08-03 09:36
浏览 1193
已采纳

QueryBuilder查询数组值

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条回答 默认 最新

  • dousou1967 2015-08-03 09:41
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法