问题描述
我的设置是Symfony 5,其最新的API平台版本在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.
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",
* })
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="Domain\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks()
*/
class User
{
private $name;
private $username;
private $email;
// ... code omitted ...
}
推荐答案
API平台默认不处理搜索过滤器中的 OR
条件,您需要使用自定义过滤器( 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/Issues/2400 .
这篇关于过滤单个查询中多个字段上的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!