问题
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?
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
}
}
}
}
My entity:
/**
* @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 ...
}
回答1:
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).
See also: https://github.com/api-platform/core/issues/2400.
来源:https://stackoverflow.com/questions/60709951/filter-query-on-multiple-fields-in-single-query