Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Using setParts in Lexik Form Filter Bundle with join in original QueryBuilder #281

Open
matteocacciola opened this issue Jan 27, 2018 · 0 comments

Comments

@matteocacciola
Copy link

matteocacciola commented Jan 27, 2018

Hello, I have a specific problem with Lexik Form Filter Bundle in Symfony 3.4. I implemented a function in order to retrieve a list of objects. It exploits joining of entities as follows:

public function getQueryBuilderMediaContents($page, $limit = 20, $orderBy = 'id', $orderType = Criteria::ASC) { $offset = ($page - 1) * $limit; $qb = $this->createQueryBuilder('mc'); $mediaContentsIds = array(); $countryIds = array(); $this->getMediaContentCountriesMaxReachPercent($mediaContentsIds, $countryIds); return $qb->select('mc, mcc, mcp, mcco') ->join(MediaContentsCampaigns::REPOSITORY, 'mcc', Expr\Join::WITH, $qb->expr()->eq('mc', 'mcc.mediaContent')) ->join('mcc.campaign', 'c') ->join(MediaContentsProducts::REPOSITORY, 'mcp', Expr\Join::WITH, $qb->expr()->eq('mc', 'mcp.mediaContent')) ->join('mcp.productGroup', 'p') ->leftJoin(MediaContentsCountries::REPOSITORY, 'mcco', Expr\Join::WITH, $qb->expr()->eq('mc', 'mcco.mediaContent')) ->join('mcco.country', 'co') ->where($qb->expr()->andX( $qb->expr()->in('mcco.mediaContent', $mediaContentsIds), $qb->expr()->in('mcco.country', $countryIds) ) ) ->setFirstResult($offset) ->setMaxResults($limit) ->orderBy($orderBy, $orderType) ->groupBy('mc.id') ; }

Please, note that $mediaContentsIds and $countryIds are passed by reference to $this->getMediaContentCountriesMaxReachPercent(). In this way, I retrieve a QueryBuilder object, which is subsequently passed to Lexif Form Filter Bundle inside a suitable Controller, as follows:

/** @var QueryBuilder $qbContents */ $qbContents = $repo->getQueryBuilderMediaContents($page, $limit, $orderBy, $orderType); $filterForm = $this->get('form.factory')->create(MediaContentsFilterType::class); if ($request->query->has($filterForm->getName())) { // manually bind values from the request $filterForm->submit($request->query->get($filterForm->getName())); // build the query from the given form object $qbUpdater = $this->get('lexik_form_filter.query_builder_updater'); $qbUpdater->addFilterConditions($filterForm, $qbNumContents); $qbUpdater->setParts(array( '__root__' => 'mc', 'mc.mediaContentCampaigns' => 'mcc', 'mc.mediaContentProducts' => 'mcp', 'mc.mediaContentCountries' => 'mco', )) // set the joins ->addFilterConditions($filterForm, $qbContents) // then add filter conditions ; }

Now, you see the way I used setParts, but I am not pretty sure about it, since the way I joined tables within getQueryBuilderMediaContents() function. On the other hand, I was forced to join tables in that way, since MediaContents entity is related to MediaContentsCampaigns, MediaContentsProducts and MediaContentsCountries entities by means of One-To-Many mappings.

Please, how to work with setParts function of Lexik FormFilter Bundle correctly, as per the case described? Am I correct, or should I change something? Thanks for your replies.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant