Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to add filter in a custom controller #360

Closed
ghost opened this issue Dec 4, 2015 · 4 comments
Closed

Impossible to add filter in a custom controller #360

ghost opened this issue Dec 4, 2015 · 4 comments

Comments

@ghost
Copy link

ghost commented Dec 4, 2015

Hello,
I'm trying to add filters in a custom controller but the initFilter function dosesn't work.
Why I'm using a customerControler?
Because I have 3 need of my application:

  1. get item by Number (unique) and not by ID
  2. use groups to give some information when you try to get a list and different information when you get a single item
  3. filter items by others parameters.

So, if I try to add filters using custom controller the bundle add the filters only to the ItemOperation method and not to the CollectionOperation.

Is this a bug?

this is my service:

  api.service:
              class: AppBundle\Services\ApiService
              arguments: ['@service_container']

  resource.store.item_operation.store_get:
          class:   "Dunglas\ApiBundle\Api\Operation\Operation"
          public:  false
          factory: [ "@api.operation_factory", "createItemOperation" ]
          arguments:
              -    "@resource.store"               # Resource
              -    [ "GET"]                 # Methods
              -    "/store/{number}"           # Path
              -    "AppBundle:Store:get"         # Controller
              -    "store_details"                 # Route name
              -    # Context (will be present in Hydra documentation)
                   "@type":       "hydra:Operation"
                   "hydra:title": "A custom operation"
                   "returns":     "xmls:string"

  resource.store.collection_operation.store_get:
              class:   "Dunglas\ApiBundle\Api\Operation\Operation"
              public:  false
              factory: [ "@api.operation_factory", "createItemOperation" ]
              arguments:
                  -    "@resource.store"               # Resource
                  -    [ "GET"]                 # Methods
                  -    "/stores"             # Path
                  -    "AppBundle:Store:getList"         # Controller
                  -    "store_list"                 # Route name
                  -    # Context (will be present in Hydra documentation)
                       "@type":       "hydra:Operation"
                       "hydra:title": "A custom operation"
                       "returns":     "xmls:string"

  resource.store:
          parent:    "api.resource"
          arguments: [ "AppBundle\Entity\Store" ]
          calls:
                      -      [ "initItemOperations", [ ["@resource.store.collection_operation.store_get"] ] ]
                      -      method:    "initFilters"
                             arguments: [ [ "@resource.store.search_filter" ] ]
                      -      [ "initCollectionOperations", [ [ "@resource.store.item_operation.store_get"] ] ]
          tags:      [ { name: "api.resource" } ]

  resource.store.search_filter:
          parent:    "api.doctrine.orm.search_filter"
          arguments: [ { siteId: "partial", name: "partial"  } ]

This is my customController:

getDoctrine()->getManager(); $store = $em->getRepository('AppBundle:Store')->findOneBySiteId($number); $groups = ['groups' => ['details']]; $resource = new Resource('AppBundle\Entity\Store'); $resource->initNormalizationContext($groups); $object = $this->findOrThrowNotFound($resource, $store->getId()); $this->get('event_dispatcher')->dispatch(Events::RETRIEVE, new DataEvent($resource, $object)); return $this->getSuccessResponse($resource, $object); } public function getListAction(Request $request) { $groups = ['groups' => ['list']]; $resource = new Resource('AppBundle\Entity\Store'); $resource->initNormalizationContext($groups); $data = $this->getCollectionData($resource, $request); if ( $request->get($this->container->getParameter('api.collection.pagination.page_parameter_name')) && 0 === count($data) ) { throw $this->createNotFoundException(); } $this->get('event_dispatcher')->dispatch(Events::RETRIEVE_LIST, new DataEvent($resource, $data)); return $this->getSuccessResponse($resource, $data, 200, [], ['request_uri' => $request->getRequestUri()]); } } ``` I would like to use the bundle because I's better to use the hidra information, the impagination and so on... Thank you so much for the answers. Regards
@ghost
Copy link
Author

ghost commented Dec 4, 2015

This is the result, I have the filter on the single item request and not in the collection.

image

@ltouro
Copy link

ltouro commented Feb 3, 2016

@alexcandy91 it has been a while but i noticed that your resource.store calls have arguments flipped between initItemOperations and initCollectionOperations

@ghost
Copy link
Author

ghost commented Feb 15, 2016

Yes I already see that.

However, thank you so much!
Regards

@soyuka
Copy link
Member

soyuka commented Feb 15, 2016

This can be closed imo

@dunglas dunglas closed this as completed Feb 15, 2016
soyuka pushed a commit to soyuka/core that referenced this issue Aug 2, 2017
soyuka pushed a commit to soyuka/core that referenced this issue Aug 3, 2017
soyuka pushed a commit to soyuka/core that referenced this issue Aug 3, 2017
soyuka pushed a commit to soyuka/core that referenced this issue Aug 4, 2017
soyuka pushed a commit to soyuka/core that referenced this issue Aug 7, 2017
soyuka pushed a commit to soyuka/core that referenced this issue Aug 7, 2017
soyuka pushed a commit to soyuka/core that referenced this issue Aug 28, 2017
soyuka pushed a commit to soyuka/core that referenced this issue Aug 28, 2017
soyuka pushed a commit to soyuka/core that referenced this issue Sep 5, 2017
dunglas added a commit that referenced this issue Sep 7, 2017
fix api-platform/#360 QueryChecker with class join
hoangnd25 pushed a commit to hoangnd25/core that referenced this issue Feb 23, 2018
hoangnd25 pushed a commit to hoangnd25/core that referenced this issue Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants