Symfony : 6.1.4 EasyAdmin :4.3.5
I try to display the products created by the logged in user. By default, all products are displayed regardless of the author.
ProductCrudController.php
class ProductCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Product::class;
}
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
$response = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
$response->andWhere('entity.creator = :creator')->setParameter('creator', ???????);
return $response;
}
[...]
}
Problem : I need to get the id of the user who created the product.
But I can't get it back via $_SESSION :
{{dump(app.user)}}
One solution I can think of is to use app.user.id in ProductCrudController.php but it is a php file. Do you have any leads to achieve this or other ideas?
CodePudding user response:
You should be able to use $this->getUser()
in your controller and retrieve the current logged in user and use it to filter your products.
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
return parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters)
->andWhere('entity.creator = :creator')
->setParameter('creator', $this->getUser());
}