-
Notifications
You must be signed in to change notification settings - Fork 736
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
Adding magic __call() #700
Conversation
I'm using Elastica with Symfony and when I try to call for a key that has ``null`` as value the Symfony throws: ``` Method "keyName" for object "Elastica\Result" does not exist in MyBundle:FolderName:myTemplate.html.twig at line X ``` Inside twig template I call like this: ```twig {{ elasticaResultVariable.keyName }} ``` Adding this magic ``__call`` it works. Maybe there is something better to do, so you can cancel this PR.
This is awful... retrieving data using magic |
@fprochazka because I'm using Is there a better and clean way do to this? Just if I set in each iteration of the loop the |
I just don't see how adding the magic call fixes it. There is already a |
@fprochazka It seems they're looking for a method, not for a property. @cassianotartari Doesn't seem to be a Elastica related issue, and adding a __call magic method is not a proper solution. Perhaps you can create your own factory class which return an array of your custom Result objects (extending Elastica\Result and implementing the magic method) based on the Elastica\ResultSet. You can complicate it as much as you want / need. Pseudocode:
|
@fprochazka adding this magic method the error stops. The only thing that I've found in twig docs is this http://twig.sensiolabs.org/doc/recipes.html#using-dynamic-object-properties talking about the already implemented magic methods Thanks @rmruano I'll study some different solution like yours |
@cassianotartari there is a difference between "an error stops" and "problem is solved". |
@fprochazka ok if you want to read the "problem is solved", the problem is solved when I add this method, call P.S.: If you have something interesting to solve the issue in a standardized way please write it down otherwise I dispense comments. |
@cassianotartari I can see what your problem is, Please notice that this is entirely an issue of your templating engine, which should probably just assume a null value if the getter method also doesn't exist instead of throwing errors. You can ask them to implement that behaviour (like @fprochazka suggested) or fall back to the solution I gave you. Implementing magic call methods is not a very good practice. |
@rmruano thanks for the clarification! |
The correct method for accessing this information in twig is |
Additionally, twig can be configured to ignore non existant variables which is talked about in the documentation (and even enabled in symfony2-standard's production mode) |
Sorry - I misunderstood the problem. The issue is twig will throw an exception on nonexistent properties by default, there are a few different ways of handling this, and my above solution won't work if the property is undefined.
|
Thanks @merk! Your first comment solves the issue with Twig. The property is already defined, comes from elasticsearch mapping. Another way that I thought was like I said before:
But again, the code would be more clean without this |
Your definition of cleanliness doesnt match mine. You want to iterate over data in an array from the result. Not a property of result. |
I'm using Elastica with Symfony and when I try to call for a key that has
null
as value inside Twig the Symfony throws:Inside twig template I call like this:
Adding this magic
__call
it works.Maybe there is something better to do, so you can cancel this PR.