omines / datatables-bundle

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

Adapter criteria not working as documented? #59

Closed 99hops closed 5 years ago

99hops commented 5 years ago

It could be just me doing something wrong but taking the "Customizing criteria" code snippet from the documentation doesn't produce anything in the end SQL query (has no effect and throws no error no matter what you type as criteria), I've tried with simple closure and as documented with no effect:

$table->createAdapter(ORMAdapter::class, [
    'entity' => Employee::class,
    'criteria' => [
        function () {
            return Criteria::create()->andWhere(new Comparison('c.name', Comparison::CONTAINS, 'ny 2'));
        },
        new SearchCriteriaProvider(),
    },
]);

What works for me is:

$this->createDataTable()
    ->createAdapter(ORMAdapter::class, [
        'entity' => SomeEntity::class,
        'criteria' => function(Doctrine\ORM\QueryBuilder $builder) use ($filters)
        {

            $criteria = Doctrine\Common\Collections\Criteria::create();

            $criteria->andWhere(
                $criteria::expr()->eq("x.y", $filters['x'])
            );

            //add composed criteria
            $builder->addCriteria($criteria);
        }
]);

But that's not the same, also "Note that implementing your own criteria overrides the default, meaning searching and sorting will no longer work automatically." doesn't seem to be true as well (at least in the above configuration) since sorting is still working with the added criteria.

omines/datatables-bundle ^0.2.1 / Symfony 4.2.2 / PHP 7.2.14

Is there something I am missing here :smiley:

curry684 commented 5 years ago

Looking at https://github.com/omines/datatables-bundle/blob/master/src/Adapter/Doctrine/ORMAdapter.php#L209-L211 it seems indeed I misdocumented that feature and you are now using it correctly, although you can still shorthand it a ton by using similar syntax to my example.

I'm not really sure why I would've made such a mistake, I'm pretty sure I constructed the samples from actual working code, but it also seems to work the same in the v0.1 branch.

curry684 commented 5 years ago

Ah found the culprit at https://github.com/omines/datatables-bundle/commit/6250255950952e6059dc582d92bd061caf2699db#diff-186d04d89cd58b4ab485e7ca6200e576L159

I apparently refactored that right before I tagged 0.1.0 hehe. PR welcome if you like :)

zeniu111 commented 5 years ago

Is it my mistake or there is something wrong with the sample in the doc? How to make cryteria work? Is there any other solution to make condition to sql query?

$table = $this->createDataTable()
             ->add('nazwaBiegu', TextColumn::class, ['label' => 'Nazwa biegu', 'className' => 'bold'])
             ->add('dyscyplina', TextColumn::class,['label' => 'dyscyplina','field' => 'u.dyscyplina',"searchable" => true] )
             ->add('nazwaBieguSkrot', TextColumn::class)
             ->createAdapter(ORMAdapter::class, [
                 'entity' => Biegi::class,
                 'query' => function (QueryBuilder $qb) {
                    $qb
                    ->select('u')
                    ->from(Biegi::class, 'u')
                    ;
                },
                 'criteria' => [
                    function () {
                        return Criteria::create()->andWhere(new Comparison('u.dyscyplina', Comparison::EQ, 'nordic'));
                    },
                    new SearchCriteriaProvider()
                ],
             ])
             ->handleRequest($request);
curry684 commented 5 years ago

Why do you reply to this issue if you're not going to read the previous replies?

curry684 commented 5 years ago

Doc issue fixed in https://github.com/omines/datatables-bundle/commit/a8a2a711a5c47a77612a232da9cf05921ce4fb07

Diarill commented 3 years ago

Hi How to parsing external variable in criteria side?

$patient = $patientRepo->findOneBy(['userId' => $code_patient]);`
$table = $this->createDataTable()
                ->add('saveAt', DateTimeColumn::class, ['label' => 'Save At', 'format' => 'd-m-Y'])
                ->add('age', TextColumn::class, ['label' => 'Age (ans)'])
                ->add('telephone', TextColumn::class, ['label' => 'Telephone'])
                ->add('seenAt', DateTimeColumn::class, ['label' => 'Seen the', 'format' => 'd-m-Y'])
                ->add('closedAt', DateTimeColumn::class, ['label' => 'Closed At', 'format' => 'd-m-Y'])
                ->add('consult', TextColumn::class, ['label' => 'Consult', 'field' => 'consult.code'])
                ->createAdapter(ORMAdapter::class, array(
                    'entity' => Parameter::class,
                    'query' => function(QueryBuilder $builder) {
                        $builder
                            ->select('p')
                            ->from(Parameter::class, 'p')
                            ->leftJoin('p.idConsult', 'consult')
                            ->leftJoin('p.idPatient', 'patient')
                            ->addSelect('consult')
                            ->addSelect('patient');
                    },
                    'criteria' => function(QueryBuilder $builder) {
                        $builder->andWhere($builder->expr()->eq('patient.id', ':id'))->setParameter('id', $patient.getId());
                    }
                ))
                ->add('status', TextColumn::class, ['label' => 'Status'])
                ->add('id', TextColumn::class, ['label' => '', 'render' => function($value, $context) {
                    return sprintf('<a href="%s" class="btn btn-xs btn-primary"><i class="fa fa-eye"></i></a>',
                        $this->generateUrl('parameter_show', ['id' => $value, 'idPatient' => $patient->getId()])
                    );
                }])
                ->handleRequest($request)
            ;`

when i run my code above i have variable not recognized as error

Diarill commented 3 years ago

when I run my code above I have variable patient not recognized as error.