symfony 1.4 - $this->getRoute()->getObject() call from helper - symfony-1.4

When I am using $this->getRoute()->getObject(), that works perfect. Is there a way to call $this->getRoute()->getObject() from helper.
When I call it from helper I got "Fatal error: Using $this when not in object context in..."

You can use:
sfContext::getInstance()->getController()->getAction(sfContext::getInstance()->getModuleName(), sfContext::getInstance()->getActionName())->getRoute()->getObject();
But it recommend you to pass the object as an argument instead of use sfContext in your helper.

Related

How to get originating context of helper in handlebars?

I am creating a custom helper.
If I use my new helper within an object like this:
{{#data}}
{{newHelper}}
{{/data}}
How do I access the data object from my helper function?
I know I can do
args.data.root['data']
But I want to access it dynamically because it wont always be within an object called 'data', it could be anything.
You can access the current context with this
So instead of using args.data.root['data'].value you can simply use this.value

Meteor check object is instance of class

In Meteor docs for matching pattern of check(value, pattern), it was mentioned:
Any constructor function (eg, Date)
Matches any element that is an instance of that type.
meaning that I can test if the value is actually an instance of my own class.
The pattern is correctly using my class constructor function as the pattern but the match failed although I am passing a class instance, something like:
check(new EventObject(param1, param2), EventObject);
where EventObject is my own class.
Did I do something wrong? Is there another way to use check() to check for class instance or should I just use my own check with instanceof?
As tested on Meteor 3.0 (3.1 was not out then), using checkon an object constructed using ES2015-style class constructor will not work as the type of the object is lost on calling Meteor.call('collection.insert', eventObject).
This can be tested by seeing that eventObject instanceof EventObject evaluates to false in Meteor.methods.

How connect autoform, meteor methods, check arguments and field with autovalue?

I'm using autoform with methods. On server side, of couse, I have to use check(item, Schema.item) method. Everything was fine, until I decided to add a field createdAt with autovalue. Now I can't pass check() in the method, because this field is not formed, and I want to insert AutoValue ... what I have to do?
You might need to call clean before you call check if you're not using collection2 which calls clean for you.
clean(doc);
clean() invokes your autoValue functions.

How can I pass a parameter to a Doctrine2 custom function in the Query Builder select() method?

In my Symfony2 project I am retrieving an ordered set of entity IDs from an Elasticsearch index. I'm then passing this list to Doctrine2 to retrieve the actual entities, by way of a WHERE IN() call.
This doesn't return them in the correct order, so I think I need to use the MySQL-specific FIELD() function. I've created a custom DQL function to allow the functionality.
So now I'm using the following code to build a Doctrine query object, but the parameters aren't being parsed into the select() method:
$itemIds = array(4,8,2,1);
$this->getRepository()
->createQueryBuilder('i')
->select('i, FIELD(i.id, :ids_string) AS HIDDEN fixed_order')
->where('i.id IN (:ids)')
->setParameters(array(
'ids_string' => implode(',', $itemIds),
'ids' => $itemIds))
->orderBy('fixed_order', 'ASC')
->getQuery()
;
This fails with the error "Invalid parameter number: number of bound variables does not match number of tokens", so apparently it's not "seeing" the :ids_string in the select() method.
I initially tried putting the FIELD() function in the orderBy() call, but it doesn't look like this is getting parsed for custom DQL function calls, and I imagine I'd run into the same problem as above.
EDIT 1 I'm aware I could put the base data directly into the select() call.
EDIT 2 I've given up and put the bare data into the select() call (which I wanted to avoid). This worked, but then it became necessary to implement Koc's suggestion of using the HIDDEN keyword to prevent Doctrine returning array(Object i, array(fixed_order)) instead of just Object i
From Doctrine 2.2 you can use HIDDEN keyword for avability field in order by without hydration them.
Try:
->select('i, FIELD(i.id, :ids_string) AS HIDDEN fixed_order')
You're going to kick yourself when you notice the problem...
Try re-reading your sentence: "so apparently it's not "seeing" the :ids_string in the select() method".
And then take a close look at your code: 'id_string' => implode(',', $itemIds)

Symfony2 Set Controller in the kernelControllerEvent using bundle:controller:action notation

I am trying to do something like the following question:
Trying to swap a controller using an event listener with Symfony2
However, when I use the code (as recommended in the answer):
$event->setController('MyMainBundle:Manage:show');
I just get an error:
LogicException: The controller must be a callable (MyMainBundle:Manage:show given).
Is there a way to use that Bundle:Controller:Method syntax in setController? Or maybe some other method I can call to resolve that to a "callable"?
What you should give to $event->setController is a callable.
What you give a string representing the logical path to a callable.
You can resolve this string using symfony's ControllerResolver.
You have to inject the controller_resolver service in your listener, and then use it like this:
$request = new Symfony\Component\HttpFoundation\Request();
$request->attributes->set('_controller', 'MyMainBundle:Manage:show'));
$event->setController($this->resolver->getController($request));
But you are clearly doing the framework's job here.

Resources