OK, so I want to send a currently logged in user to my custom method in ProductRepository, that will filter the products for the ones that are assigned to this user. My product.yml route to the listing of products:
sylius_backend_product_index:
pattern: /
methods: [GET]
defaults:
_controller: sylius.controller.product:indexAction
_sylius:
template: SyliusWebBundle:Backend/Product:index.html.twig
method: createFilterPaginator2
arguments: [$criteria, $sorting, $deleted, $user]
Next I go to the controller, ProductController does not have the indexAction method, but its extension ResourceController has, the index action:
public function indexAction(Request $request)
{
$criteria = $this->config->getCriteria();
$sorting = $this->config->getSorting();
$user = $this->get('security.context')->getToken()->getUser();
$repository = $this->getRepository();
if ($this->config->isPaginated()) {
$resources = $this->resourceResolver->getResource(
$repository,
'createFilterPaginator2',
array($criteria, $sorting, false, $user)
);
$resources->setCurrentPage($request->get('page', 1), true, true);
$resources->setMaxPerPage($this->config->getPaginationMaxPerPage());
if ($this->config->isApiRequest()) {
$resources = $this->getPagerfantaFactory()->createRepresentation(
$resources,
new Route(
$request->attributes->get('_route'),
$request->attributes->get('_route_params')
)
);
}
} else {
$resources = $this->resourceResolver->getResource(
$repository,
'findBy',
array($criteria, $sorting, $this->config->getLimit(), $user)
);
}
$view = $this
->view()
->setTemplate($this->config->getTemplate('index.html'))
->setTemplateVar($this->config->getPluralResourceName())
->setData($resources)
;
return $this->handleView($view);
}
Here I added the
$user = $this->get('security.context')->getToken()->getUser();
and then added $user to the parameters array for the $resources. The createFilterPaginator2 method itself looks like :
public function createFilterPaginator2($criteria = array(), $sorting = array(), $deleted = false, UserInterface $user = NULL)
{
$queryBuilder = parent::getCollectionQueryBuilder()
->select('product, variant')
->leftJoin('product.variants', 'variant')
->where('product.seller = :user')
->setParameter('user', $user)
;
//var_dump($user->getUsername());
if (!empty($criteria['name'])) {
$queryBuilder
->andWhere('product.name LIKE :name')
->setParameter('name', '%'.$criteria['name'].'%')
;
}
if (!empty($criteria['sku'])) {
$queryBuilder
->andWhere('variant.sku = :sku')
->setParameter('sku', $criteria['sku'])
;
}
if (empty($sorting)) {
if (!is_array($sorting)) {
$sorting = array();
}
$sorting['updatedAt'] = 'desc';
}
$this->applySorting($queryBuilder, $sorting);
if ($deleted) {
$this->_em->getFilters()->disable('softdeleteable');
}
return $this->getPaginator($queryBuilder);
}
And the problem is that in the ResourceController my $user correctly returns current logged in user, but the $user isn't successfully sent to the method. I believe I'm missing sth important, my guess would be that the arguments: [$criteria, $sorting, $deleted, $user] shouldn't be returned by the ResourceController, but I have no better idea atm.
I would appreciate any help, on how to send parameters to the custom repository methods, useful links etc. Thanks !
Have you updated your Product Model? It doesn't include a user field by default...
I haven't tried it myself, but check out jbinfo's answer here: https://github.com/Sylius/Sylius/issues/1827
In case that page disappears his example was:
acme_account_company_list:
pattern: /list
defaults:
_controller: sylius.controller.company:indexAction
_sylius:
method: findAllCompaniesByUser
arguments: ['expr:service("security.context").getToken().getUser()']
paginate: 10
sorting:
updatedAt: desc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.