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

Parse error when using custom operator #40

Open
realshadow opened this issue Mar 24, 2016 · 2 comments
Open

Parse error when using custom operator #40

realshadow opened this issue Mar 24, 2016 · 2 comments

Comments

@realshadow
Copy link

Hey,

I am trying to create a custom operator for range (between), but I always end up with the same error message

"Parse error: syntax error, unexpected '\"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)"

which occurs in the compiled file /tmp/rulerz_executor_34868ebc.

Looking at the source, it generates

return "call_user_func($operators["range"], @@_ROOT_ALIAS_@@.createdAt, :from)";

which is not a valid code. I managed to fix this by changing the source to

return call_user_func($operators["range"], "@@_ROOT_ALIAS_@@.createdAt", ":from");

after this change my callback gets called (but wrong data is passed in due to eval issues I guess)

This is my initialization

new RulerZ(
    $compiler,
    [
        new DoctrineQueryBuilderVisitor([
            'range' => function($a, $b) {
                die('foo!');
            }
        ]),
        new ArrayVisitor(),
    ]
);

I am not sure if this is a bug or I am doing something wrong.

Any ideas?

@K-Phoen
Copy link
Owner

K-Phoen commented Apr 3, 2016

As I explained in another issue, there are in fact two sort of operators in RulerZ: the classic ones and the inline ones.

When using classic operators, we - in fact - refer to a PHP callback that will be called at runtime. It's particularly useful for the ArrayVisitor because it compile rules to plain PHP code.
On the other hand, when you use inline operators, the callback provided in the operator's definition is used at compile-time to generate code. That's how all the built-in SQL operators are implemented in RulerZ and that's what you will want to use most of the time (in an SQL context).

Your range definition should look like this:

$visitor = new DoctrineQueryBuilderVisitor();
$visitor->setInlineOperator('range', function($a, $b) {
    return 'SQL code here';
});

That being said, classic operators should work even for SQL targets. In that regard, I consider this issue as a bug. I doubt that fixing it would be trivial though.
And in addition to this bug, the documentation is clearly not as good as it should be (I'll create a new issue to fix that).

@realshadow
Copy link
Author

I figured it out a bit later :)

I can only confirm the bug as I could not get past the syntax error with both compilers.

Thanks for looking at it

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

No branches or pull requests

2 participants