I'm working on some API Platform project.
I 'm using my API point/references
to get some data containing subentities.
But the issue is that I use a Doctrine extension to filter my list. Using this extension I can filter which references I get.
But in the subentity Stock I stil have some data that I dont want.
My goal is to get all stocks that have the warehouse.owner = $owner.
Here is the code :
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', Expr\Join::WITH, 'w.owner = :owner');
$queryBuilder->setParameter('owner', $client->getSubject());
}
Here is the response :
{
"@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"
},
How can I filter the subentity result by just using my extension and DQL ? Thanks
Extension is used for retrieve main items, not associated collection.
Instead you can use data transformer.
class ReferenceOutput {
public $stocks;
}
//Next class
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use Symfony\Component\Security\Core\Security;
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;
}
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Symfony compares favorably with other PHP frameworks in terms of reliability and maturity. This framework appeared a long time ago, in 2005, that is, it has existed much longer than most of the other tools we are considering. It is popular for its web standards compliance and PHP design patterns.
https://symfony.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.