如何 select 只有在 collectionOperations 中接受属性值为 true 的书籍才能使用 Symfony Api 平台
How to select only books having attribute accepted true as value in collectionOperations get with Symfony Api Platform
我正在使用 Symfony4 和 Symfony Api 平台,我想知道如何 select 只有具有属性 "accepted" true 作为所有用户的价值,但 select 所有书籍,如果用户有 ROLE_ADMIN。我应该使用 Api 平台的哪个组件?
* @ApiResource(
* attributes={
* "pagination_items_per_page"=20,
* "order"={"id": "DESC"},
* },
* collectionOperations={
* "get"={
* "normalization_context"={"groups"={"book:read"}}
* },
* "post"={
* "denormalization_context"={"groups"={"book:write"}},
* "security"="is_granted('POST', object)",
* "security_message"="You do not have the rights to post"
* },
* },
* @ORM\Entity
*/
class Book
{
//**
/**
* @ORM\Column(type="boolean" , nullable=true)
* @Groups({"book:read", "admin:input"})
*/
private $accepted = false;
我找到了使用 extensions 的解决方案:
https://api-platform.com/docs/core/extensions/
final class BookListingIsEnabledExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null): void
{
$this->addWhere($queryBuilder, $resourceClass);
}
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void
{
$this->addWhere($queryBuilder, $resourceClass);
}
private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void
{
if (Book::class !== $resourceClass || $this->security->isGranted('ROLE_ADMIN')) {
return;
}
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->andWhere(sprintf('%s.accepted = :accepted', $rootAlias))
->setParameter('accepted', true);
}
}
我正在使用 Symfony4 和 Symfony Api 平台,我想知道如何 select 只有具有属性 "accepted" true 作为所有用户的价值,但 select 所有书籍,如果用户有 ROLE_ADMIN。我应该使用 Api 平台的哪个组件?
* @ApiResource(
* attributes={
* "pagination_items_per_page"=20,
* "order"={"id": "DESC"},
* },
* collectionOperations={
* "get"={
* "normalization_context"={"groups"={"book:read"}}
* },
* "post"={
* "denormalization_context"={"groups"={"book:write"}},
* "security"="is_granted('POST', object)",
* "security_message"="You do not have the rights to post"
* },
* },
* @ORM\Entity
*/
class Book
{
//**
/**
* @ORM\Column(type="boolean" , nullable=true)
* @Groups({"book:read", "admin:input"})
*/
private $accepted = false;
我找到了使用 extensions 的解决方案:
https://api-platform.com/docs/core/extensions/
final class BookListingIsEnabledExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null): void
{
$this->addWhere($queryBuilder, $resourceClass);
}
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void
{
$this->addWhere($queryBuilder, $resourceClass);
}
private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void
{
if (Book::class !== $resourceClass || $this->security->isGranted('ROLE_ADMIN')) {
return;
}
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->andWhere(sprintf('%s.accepted = :accepted', $rootAlias))
->setParameter('accepted', true);
}
}