omines / datatables-bundle

DataTables bundle for Symfony
https://omines.github.io/datatables-bundle/
MIT License
258 stars 115 forks source link

ORMAdapter with repositories #61

Closed mquestionable closed 5 years ago

mquestionable commented 5 years ago

Hello,

Is there any example on how I can make this work with ORMAdapter when using EntityRepository? Ex. Controller

$jobs = $this->jobRepository->findByManager($currentUser);
$table = $dataTable->create()
            ->add('id', TextColumn::class)
            ->createAdapter(ORMAdapter::class, [
                  'entity' => Job::class,
                  'query' => $jobs
            ])
            ->handleRequest($request);

JobRepository

public function findByManager(User $manager)
    {
        $queryBuilder = $this->createQueryBuilder('j');
        $queryBuilder->select('j')
            (...joins and where...)
            ->getQuery();
        return $queryBuilder->getQuery()->getResult();
    }

This with ->getResult() won't obviously work, I tried returning only query but nothing. I did succeeded with using ArrayAdapter and using getArrayResult to return but I need objects as I call some additional checks on models.

Thanks

curry684 commented 5 years ago

I'm not sure what you would expect of it. The point of an Entity Repository is to bundle and parametrize reusable queries centrally. The point of a QueryBuilder is to construct dynamic queries at runtime with flexible parameters. As DataTables inherently need to sort and filter dynamically the ORMAdapter plugs in at the QueryBuilder level where it can manage those things.

To integrate with repositories you're likely better of just using an ArrayAdapter as you wouldn't be using any of the "magic" that makes ORMAdapter tick.

curry684 commented 5 years ago

Essentially duplicate of https://github.com/omines/datatables-bundle/issues/67 and same answer applies.