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

\MonterHealth\ApiFilterBundle\MonterHealthApiFilter::applyFilterResults does not deal correctly properties annotated with \Doctrine\ORM\Mapping\Embedded #6

Open
ste80pa opened this issue Dec 26, 2021 · 3 comments

Comments

@ste80pa
Copy link

ste80pa commented Dec 26, 2021

Doctrine has a cool feature for separating concerns called Embeddables

take in example the scenario described at https://www.doctrine-project.org/projects/doctrine-orm/en/2.10/tutorials/embeddables.html and add a cotroller to retrieve users by api

<?php
use Doctrine\ORM\Mapping as ORM;
use MonterHealth\ApiFilterBundle\Annotation\ApiFilter;
use MonterHealth\ApiFilterBundle\Filter\SearchFilter;
/** 
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @ApiFilter(SearchFilter::class, properties={
 *     "address"
 *
 * }) */
class User
{
    /** @ORM\Embedded(class = "Address") */
    private $address;
}
<?php
use Doctrine\ORM\Mapping as ORM;
use MonterHealth\ApiFilterBundle\Annotation\ApiFilter;
use MonterHealth\ApiFilterBundle\Filter\SearchFilter;

/** @ORM\Embeddable */
class Address
{
    /** @ORM\Column(type = "string") */
    private $street;

    /** @ORM\Column(type = "string") */
    private $postalCode;

    /** @ORM\Column(type = "string") */
    private $city;

    /** @ORM\Column(type = "string") */
    private $country;
}
<?php
namespace App\Controller;

use App\Repository\UserRepository;
use MonterHealth\ApiFilterBundle\MonterHealthApiFilter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class UserController extends AbstractController
{
    /**
     * @Route("users", name="get_users", methods={"GET"})
     * @param Request $request
     * @param UserRepository $repository
     * @param MonterHealthApiFilter $monterHealthApiFilter
     * @return JsonResponse
     * @throws \ReflectionException
     */
    public function getBooks(Request $request, UserRepository $repository, MonterHealthApiFilter $monterHealthApiFilter): JsonResponse
    {
        $queryBuilder = $repository->findAllQueryBuilder();
        $monterHealthApiFilter->addFilterConstraints($queryBuilder, $repository->getClassName(), $request->query);

        return new JsonResponse($queryBuilder->getQuery()->getArrayResult());
    }
}

now after everything it is in place we invoke the api and what we expect it i to obtain users filtered by address.city = "Rome"

curl -X 'GET' 'http://127.0.0.1:8080/users?address:city=Rome&limit=20&page=1'

what happens in reality it is that the filter it is simply ignored or confused with a join.

After some debug i wrote a solution that i will submit soon

ste80pa added a commit to ste80pa/api-filter-bundle that referenced this issue Dec 26, 2021
Add Doctrine Embeddable Support
@ste80pa ste80pa changed the title \MonterHealth\ApiFilterBundle\MonterHealthApiFilter::applyFilterResults does not deal correctly poperties annotated with \Doctrine\ORM\Mapping\Embedded \MonterHealth\ApiFilterBundle\MonterHealthApiFilter::applyFilterResults does not deal correctly properties annotated with \Doctrine\ORM\Mapping\Embedded Dec 27, 2021
@monterhealth
Copy link
Owner

monterhealth commented Sep 2, 2024

@ste80pa: Is this wish still relevant? If so, we will add the option in the latest version that uses attributes.

@ste80pa
Copy link
Author

ste80pa commented Oct 5, 2024

@monterhealth yes it is still relevant :D

@monterhealth
Copy link
Owner

@ste80pa Ok, we'll put it on our list!

@monterhealth monterhealth reopened this Oct 5, 2024
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

2 participants