如何在Symfony2中将参数发送到自定义存储库方法?

时间:2014-08-18 07:39:47

标签: php symfony sylius

好的,所以我想将当前登录的用户发送到ProductRepository中的自定义方法,该方法将过滤分配给该用户的产品。我的product.yml产品列表的路线:

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]

接下来我去控制器,ProductController没有indexAction方法,但它的扩展名ResourceController有,索引动作:

 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);
}

我在这里添加了

 $user = $this->get('security.context')->getToken()->getUser();

然后将$ user添加到$ resources的参数数组中。 createFilterPaginator2方法本身如下:

 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);
}

问题是在ResourceController中我的$ user正确返回当前登录的用户,但是$ user没有成功发送到该方法。我相信我错过了重要的事情,我的猜测是:[$ criteria,$ sorting,$ deleted,$ user]的参数不应该由ResourceController返回,但我没有更好的主意atm

我将不胜感激,如何将参数发送到自定义存储库方法,有用的链接等等。谢谢!

2 个答案:

答案 0 :(得分:0)

您是否更新了产品型号?默认情况下,它不包含用户字段...

答案 1 :(得分:0)

我自己没试过,但请查看jbinfo的答案: https://github.com/Sylius/Sylius/issues/1827

如果该页面消失,他的例子是:

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