Skip to content

Commit 6411e31

Browse files
committed
add an ability to set default filters. fix #52
1 parent 3c08deb commit 6411e31

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ Below line has additional condition: books which have "php" in their title.
145145
http://example.com/books?filter[title]=%php%&filter[authors.name]=hamid%
146146
```
147147
148+
#### Setting a default filter on the IndexAction
149+
By using ```$resourceCollection->getQuery()``` you can get access on the query object.
150+
use "r" alias for referring to the current entity. in this example the "r" refers to the "ProjectEntity"
151+
```php
152+
class ProjectController extends Controller
153+
{
154+
/**
155+
* @Route("/", name="projects_index", methods="GET")
156+
*/
157+
public function index(ProjectRepository $projectRepository, ResourceCollection $resourceCollection): ResponseInterface
158+
{
159+
$resourceCollection->setRepository($projectRepository);
160+
161+
$resourceCollection->getQuery()->where('r.user_id = :s1')->setParameter(...);
162+
$resourceCollection->handleIndexRequest();
163+
164+
return $this->jsonApi()->respond()->ok(
165+
new ProjectsDocument(new ProjectResourceTransformer()),
166+
$resourceCollection
167+
);
168+
}
169+
```
170+
148171
#### Other Finders
149172
Currently the following Finders are available via other bundles:
150173

src/Helper/ResourceCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,16 @@ public function getRepository(): EntityRepository
7272
}
7373

7474
/**
75-
* Sets the Repository.
75+
* Sets the Repository And makes query_object.
7676
*
7777
* @param entityRepository $repository
7878
* The Repository
7979
*/
8080
public function setRepository(EntityRepository $repository): void
8181
{
8282
$this->repository = $repository;
83+
84+
$this->query = $this->generateQuery();
8385
}
8486

8587
/**
@@ -102,8 +104,6 @@ public function getQuery(): QueryBuilder
102104
*/
103105
public function handleIndexRequest()
104106
{
105-
$this->query = $this->generateQuery();
106-
107107
$entityManager = $this->query->getEntityManager();
108108
$this->fieldManager->setEntityManager($entityManager);
109109
$this->fieldManager->setRootEntity($this->query->getRootEntities()[0]);

0 commit comments

Comments
 (0)