php - How can I send a parameter into custom repository method in Symfony2? -
ok, want send logged in user custom method in productrepository, filter products ones assigned user. product.yml route 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 go controller, productcontroller not have indexaction method, extension resourcecontroller has, 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 added the
$user = $this->get('security.context')->gettoken()->getuser();
and added $user parameters array $resources. createfilterpaginator2 method looks :
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 :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 problem in resourcecontroller $user correctly returns current logged in user, $user isn't sent method. believe i'm missing sth important, guess arguments: [$criteria, $sorting, $deleted, $user] shouldn't returned resourcecontroller, have no better idea atm.
i appreciate help, on how send parameters custom repository methods, useful links etc. !
have updated product model? doesn't include user field default...
Comments
Post a Comment