-
Notifications
You must be signed in to change notification settings - Fork 26
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
Filtering by calculated fields #34
Comments
Hi, Actually, at this moment there is no way. # config/services.yaml
parameters:
jsonapi-bundle.filtering.custom-fields:
App/Entity/EntityName:
tags:
type: string and return these fields before this: jsonapi-bundle/src/Helper/FieldManager.php Lines 240 to 243 in 34ecf12
|
Thank you for your answer! I tries the solution you suggested and it did get me pass this error, but the code now breaks inside doctrine:
I am aware this is because it does not know how to query the calculated value, but I am not sure what would be the correct way to write the queries for custom filtering. So far, I extended the Do you have a better idea? |
I've developed a new feature for filtering by subQueries. for instance: if you had an authors table and 2 fields "firstName" and "lastName", and you want to have a filter by "fullName", then you had to define a new method in AuthorsRepository like this: public function filterByFullName()
{
$qb = $this->createQueryBuilder('af');
$qb
->select('CONCAT(af.fistName, af.lastName)')
->where('af.id = r.id');
return [
SubQueryManager::TYPE => 'string',
SubQueryManager::DQL => $qb->getDQL(),
];
} then:
@mnugter could you please check it out? |
I read through the branch and it seems like a cool feature! Code seems good and it's a clean addition (not breaking any current functionalities). I'm not 100% sure yet on the location of the filterBy method. This would pollute the EntityRepository with functions that are not directly useable on the repository (you cannot call $repository->filterByFullname() as it does not give a relevant return value). Could this have a place in the transformers? They do handle everything related to a request and you have them for each endpoint / entity. |
For anyone else trying to find a solution, this is the one I used. In my entity I have the field I've overriden the finder serivce paknahad_json_api.helper_filter.finder:
class: App\JsonApi\Helper\Finder
tags: ['paknahad.json_api.finder']
<?php
declare(strict_types=1);
namespace App\JsonApi\Helper;
use Paknahad\JsonApiBundle\Helper\Filter\Finder as BaseFinder;
class Finder extends BaseFinder
{
/**
* @inheritDoc
*/
protected function setCondition(string $field, string $value): void
{
if (!in_array($field, ['dateFrom', 'dateTo'], true)) {
parent::setCondition($field, $value);
return;
}
$this->fieldManager->addField('date');
$this->query->andWhere(sprintf(
'%s %s= :%s',
$this->fieldManager->getQueryFieldName('date'),
$field === 'dateFrom' ? '>' : '<',
$field
));
$this->query->setParameter($field, $value);
}
} Please note that you have to override the existing finder service, else if you just tag your custom finder (as per the documentation) it wont work, as they both will add the fields (the original finder will add |
Hi,
we have been using your bundle for a while and it's great, but today I came across a problem of filtering by calculated field. I would like to filter the list of my Offer entities by dynamic field
tags
. Tags are detected by some complex logic and not stored to database, but added on the fly. I found a way to do the filtering by extending thePaknahad\JsonApiBundle\Helper\ResourceCollection
, but the bundle does not allow me to add?filter[tag]=tagName
to request because it is not an actual field in the table. The error message I am getting is:Is there a way to achieve custom filters?
Thanks
The text was updated successfully, but these errors were encountered: