在单个查询中对多个字段进行过滤查询 [英] filter query on multiple fields in single query

查看:32
本文介绍了在单个查询中对多个字段进行过滤查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置是 Symfony 5,最新的 API-Platform 版本在 PHP 7.3 上运行.所以我希望能够查询姓名和用户名(甚至电子邮件).我需要编写自定义解析器吗?

My setup is Symfony 5 with the latest API-Platform version running on PHP 7.3. So I would like to be able to query both on name and username (maybe even email). Do I need to write a custom resolver?

这是我迄今为止尝试过的,但结果是 WHERE name = $name AND username = $name.

This is what I've tried so far but this results in a WHERE name = $name AND username = $name.

query SearchUsers ($name: String!) {
  users(name: $name, username: $name) {
    edges {
       cursor
       node {
         id
         username
         email
         avatar
       }
     }
  }
}

我的实体:

/**
 * @ApiResource
 * @ApiFilter(SearchFilter::class, properties={
 *   "name": "ipartial",
 *   "username": "ipartial",
 *   "email": "ipartial",
 * })
 *
 * @ORMTable(name="users")
 * @ORMEntity(repositoryClass="DomainRepositoryUserRepository")
 * @ORMHasLifecycleCallbacks()
 */
class User
{
  private $name;
  private $username;
  private $email;
  // ... code omitted ...
}

推荐答案

搜索过滤器中的 OR 条件在 API Platform 中默认不处理,您需要一个自定义过滤器来执行此操作 (https://api-platform.com/docs/core/filters/#creating-custom-filters).

The OR condition in the search filter is not handled by default in API Platform, you need a custom filter to do this (https://api-platform.com/docs/core/filters/#creating-custom-filters).

另见:https://github.com/api-platform/core/问题/2400.

这篇关于在单个查询中对多个字段进行过滤查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆