omines / datatables-bundle

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

Question or Issue? using variable in query #56

Closed ISeeTWizard closed 5 years ago

ISeeTWizard commented 5 years ago

Hi, again me... This time I'm not sure if it's an issue or Question The point is the following is working: ->createAdapter(ORMAdapter::class, [ 'entity' => Systems::class, 'query' => function (QueryBuilder $builder) { $builder ->select('systems') ->from(Systems::class, 'systems') ->orderBy('systems.level', 'ASC') ->addorderBy('systems.label', 'ASC') ->andwhere('systems.territory = :SearchTerritory') ->setParameter('SearchTerritory', 'Independent'); }, But when I replace 'Independent' with a variable like $territory which is given globally to my controller this doesn't work. Is there a restriction from you component here because I searched now for a while and everywhere it's marked that this should work with the querybuider... Here I'm just getting plenty jquery errors :( Looking at those I see Notice: Undefined variable: territory but dumping the variable before I create the table is working, so the variable is correctly set. I suppose somehow the variable is not send over to the table but why I just can say ????

curry684 commented 5 years ago

In PHP anonymous functions have isolated scope, so inside the function there is no knowledge about variables in the containing function, like in this case $independent. This is consistent with most language like C# and Java actually, only Javascript has peculiar scoping where this would work.

To pass variables into the function's scope you must use them, see example 3 in the docs.

As this is not a bundle issue I'm closing it as answered.

ISeeTWizard commented 5 years ago

I see what you mean. I never had to use the ´use´ statement until now so I didn’t know that this must be done like this. Thank you very much for your kindness and patience and sorry for all my questions.

curry684 commented 5 years ago

Happy to help, no prob 😄

MVeizaj commented 5 years ago

I am having the same issue and tried this solution and still nothing, @curry684

->createAdapter(ORMAdapter::class, [
                'entity' => Property::class,
                'query' => function (QueryBuilder $builder) use ($user) {
                    $builder
                        ->select('property')
                        ->from(Property::class, 'property')
                        ->andwhere('property.user = :user')
                        ->setParameter('user', $user);
                        }
                    ])
curry684 commented 5 years ago

I would expect that code to work fine. You'll need to provide some debugging information if it doesn't.

MVeizaj commented 5 years ago

ok I fixed I had to declare even the join in the query. Thanks

curry684 commented 5 years ago

You're providing your own query builder so you have to provide all of it 😄 good to hear it's fixed!