I'm trying to add a distance filter on my entity using api-platform (2.6.5). Everything works fine, except for the response body :
"hydra:member": [
{
"0": {
"@id": "/api/places/1",
"@type": "Place",
"id": 1,
"name": "test",
"description": "test",
"slug": "test",
"address": "test",
"country": "Suisse",
"latitude": "50.6300000",
"longitude": "3.0620000",
"tableCount": 10,
"tablePlace": 6,
"tableCapacity": 60
},
"distance": "8.887448516728334"
},
{
"0": {
"@id": "/api/places/8",
...
},
"distance": "10.273770606986691"
},
{...}
The calculated distance is not considered like an entity property and api platform return them separately (This is a problem for the frontend).
Here is my entity :
/**
* @ORM\Entity(repositoryClass=PlaceRepository::class)
* @ApiResource(
* normalizationContext={"groups"={"place", "place:read"}, "swagger_definition_name": "Read"},
* denormalizationContext={"groups"={"place", "place:write"}, "swagger_definition_name": "Write"},
* )
* @ApiFilter(SearchFilter::class, properties={"name": "partial", "description": "partial", "zip":"start", "city": "exact"})
* @ApiFilter(NearbyFilter::class, properties={"location"})
* @Entity @HasLifecycleCallbacks
*/
class Place
{
....
/**
* @ORM\Column(type="decimal", precision=10, scale=7, nullable=true)
* @Groups({"place"})
*/
private $latitude;
/**
* @ORM\Column(type="decimal", precision=10, scale=7, nullable=true)
* @Groups({"place"})
*/
private $longitude;
/**
* @var float The distance calculated
* @Groups({"place:read"})
*/
private $distance;
And here is my filter :
final class NearbyFilter extends AbstractContextAwareFilter
{
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
if ($property !== 'location') {
return;
}
[$lat, $lng, $radius] = explode(',', $value);
// add distance in DQL based on filter location point
$queryBuilder
->addSelect('
(6371.0 * acos (
cos ( radians(:latitude) )
* cos( radians(o.latitude) )
* cos( radians(o.longitude) - radians(:longitude) )
+ sin ( radians(:latitude) )
* sin( radians(o.latitude) )
)) AS distance')
->having('distance <= :radius')
->setParameter('radius', $radius)
->setParameter('latitude', $lat)
->setParameter('longitude', $lng)
->orderBy('distance')
;
}
As you can see, i tried to add unmapped property 'distance' but that have no effect :/ I also tried to use Dto and Denormalizer to fix this without success (But I'm new with it).
Can you have an idea to fix my problem ?
Thanks !
PrePersistandPreUpdateentity event listener or subscriber to perform the calculation and save the distance to the database as a persisted decimal value. Instead of using a listener you could add the calculation to the setter methods in your entity, but may pose consistency issues if all the values are not set. - Will B.