API平台的条令扩展-SQL



我正在做一些API平台项目。我正在使用我的API点/references来获取一些包含子元素的数据。

但问题是,我使用条令扩展来过滤我的列表。使用这个扩展,我可以过滤我得到的引用。

但在股票这一子实体中,我仍然有一些我不想要的数据。

我的目标是获得所有拥有仓库的股票。owner=$owner

这是代码:

private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void
{
if ($this->security->isGranted('SCOPE admin') || !$this->security->isGranted('SCOPE logistician')) {
return;
}
/** @var Client $client */
$client = $this->security->getUser();
$rootAlias = $queryBuilder->getRootAliases()[0];
switch ($resourceClass) {
case Entry::class:
case Inventory::class:
$queryBuilder->join(sprintf('%s.stock', $rootAlias), 's');
break;
case Reference::class:
// I'm here
$queryBuilder->join(sprintf('%s.stocks', $rootAlias), 's');
break;
default:
return;
break;
}
$queryBuilder->join('s.warehouse', 'w', ExprJoin::WITH, 'w.owner = :owner');
$queryBuilder->setParameter('owner', $client->getSubject());
}

以下是回复:

{
"@id": "/stock/references/7a00e32f-1195-43e6-a5d1-f75d64471112",
"@type": "Reference",
"id": "7a00e32f-1195-43e6-a5d1-f75d64471112",
"quantityAvailable": 75,
"quantitiesOnHand": 100,
"quantityOfExpectedEntries": -25,
"archived": false,
"stocks": [
{
"@id": "/stock/stocks/214d9b27-d2c8-45e5-9d67-10985291022a",
"@type": "Stock",
"quantityOnHand": 50,
"lastCountedEntryWasCreatedAt": "2020-07-23T10:04:32+02:00",
"warehouse": {
"@id": "/stock/warehouses/3a61275f-4b20-4061-a64e-52783cf4d892",
"@type": "Warehouse",
"id": "3a61275f-4b20-4061-a64e-52783cf4d892",
"owner": "9001",
"name": "Adams-Reichel",
"createdAt": "2020-07-23T10:04:32+02:00"
},
"createdAt": "2020-07-23T10:04:32+02:00"
},
{
"@id": "/stock/stocks/6f2a0542-d65d-489a-b96c-c8658ff195ea",
"@type": "Stock",
"quantityOnHand": 50,
"lastCountedEntryWasCreatedAt": "2020-07-23T10:04:32+02:00",
"warehouse": {
"@id": "/stock/warehouses/cc8f3267-29b6-4ad5-9f8b-74b98aab85d6",
"@type": "Warehouse",
"id": "cc8f3267-29b6-4ad5-9f8b-74b98aab85d6",
"owner": "9002",
"name": "Steuber, Ruecker and Vandervort",
"createdAt": "2020-07-23T10:04:32+02:00"
},
"createdAt": "2020-07-23T10:04:32+02:00"
}
],
"createdAt": "2020-07-23T10:04:32+02:00"
},

如何仅使用我的扩展名和DQL来过滤子实体结果?感谢

Extension用于检索主项,而不是关联的集合。

相反,您可以使用数据转换器。

class ReferenceOutput {
public $stocks;
}
//Next class
use ApiPlatformCoreDataTransformerDataTransformerInterface;
use SymfonyComponentSecurityCoreSecurity;
class ReferenceOutputTransformer implements DataTransformerInterface
{
private string $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* Transforms the given object to something else, usually another object.
* This must return the original object if no transformations have been done.
*
* @param object $object Snapshot
*
* @param string $to
* @param array $context
*
* @return object
*/
public function transform($object, string $to, array $context = [])
{
/** @var Reference $reference */
$reference = $object;
/** @var Client $client */
$client = $this->security->getUser();
$referenceOutput = new ReferenceOutput();
$stocks = $reference->getStoks()->filter(
static function($key, $stock) use ($client) {
return $stock->getWarehouse()->getOwner() === $client->getSubject();
});
$referenceOutput->stocks = $stocks;
return $referenceOutput;
}
/**
* Checks whether the transformation is supported for a given data and context.
*
* @param object|array $data object on normalize / array on denormalize
*
* @param string $to
* @param array $context
*
* @return bool
*/
public function supportsTransformation($data, string $to, array $context = []): bool
{
return ReferenceOutput::class === $to && $data instanceof Reference;
}
}

相关内容

  • 没有找到相关文章

最新更新