Adding filter criteria to search api

I’m working on a module that organizes user, resources and sites into related teams. I’m trying to modify the search api so that the results returned are filtered against the user’s current default team, or another of their teams they select in the advanced search. Right now I’m trying to accomplish this by using a join statement

                 $qb->innerJoin(
                     'Teams\Entity\TeamResource', 
                     'tr', 
                     Expr\Join::WITH, 
                     $entityClass . '.id = tr.resource')
                     ->where('tr.team = :team_id')
                    ->setParameter('team_id', $team_id);

The TeamResources table contains columns with Omeka resource ids and team ids. The $team_id variable is returned by the advanced search form. I’m using the api.search.query event to inject this join statement.

The join seems to work when displaying all of the items/item sets/media/resource templates, but fails when using the advanced search to filter by value, class or template (filtering items by item set works). I get the error:

 **Doctrine\ORM\Query\QueryException**
Too many parameters: the query defines 1 parameters and you bound 2

I’ve only tried this with the item advanced search so far. The error message always says that the query defines 1 + the number of item sets selected in the advanced search, and that I bound that number + the number of value, class and template parameters I selected in the advanced search. So, it seems like my join statement is interfering with building the property query, but still allowing the parameters for the property query to be bound.

I’m having a hard time debugging this. Is there somewhere I should look to get a better understanding of how the advanced search queries are built? Is there a more appropriate way to add a filter criteria to the search api?

I think your problem is probably that you’re using where instead of andWhere: where replaces any existing where-clauses, while andWhere adds on with AND to whatever’s already there. You’re accidentally removing the where clauses added by the other filters that are running, and Doctrine is complaining because the parameter placeholders that were in those where clauses are now gone.

In general usage there’s really no reason to use where: andWhere works fine if its the first where clause, and there’s orWhere in the alternative if you need an OR. Only when you’re really trying to totally alter an existing query should you be using where.

1 Like

Oh–thank you so much! That’s it. Apologies for basically asking you to debug my code. I’ll have to take another pass as the Doctrine documentation.

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.