3 users online. Create an account or sign in to join them.Users
Pass a parameter from an event to a data source
This is an open discussion with 9 replies, filed under General.
Search
I've tried doing this using PHP and a global variable and a session variable, but it seems the data source is executed before the event is run.
This shouldn't be the case. The order in which you see events and data sources in your XML is generally the execution order: events first, then data sources. If you put die('event') and die('data source') in the respective files you should see event first. I hope!
I have used a similar technique to the one you describe, by stuffing things inside of the $_GET superglobal:
$_GET['my-project-namespace']['my-variable'] = 'foo';
Which can then be picked up in a later file. It's a bit hacky, but it works.
Datasources are always executed after events, so there must be something wrong. You should be careful using $_SESSION as symphony overrides the default php behaviour and you might get unexpected results.
I don't know how proficient you are in writing php, but you could use a static property of either your datasource or your event to store the data you need.
EDIT beaten by Nick :D
Thanks for your replies. I don't exactly know what happened, but you were right, the order was just the way I needed it to be, something else was apparently wrong.
@alpacaaa: how would I access the event object from within the data-source? Is is available via some global variable?
Nope, it isn't in the global scope (fortunately). To follow the static route, you'd need to add a static variable to your event class, like this:
public static $myvar = null;
Then set its value where appropriate:
self::$myvar = 'myvalue';
And then, from your datasource, access the value like this:
MyEventClassName::$myvar // 'myvalue'
This approach is not properly OOP and could be improved a lot, but works and is rather simple :)
This approach is not properly OOP and could be improved a lot, but works and is rather simple :)
I wonder how could this be made properly OOP?
There's nothing wrong with this approach, unless you care about concepts like encapsulation, data hiding and the like...
I'm not an OOP purist, if you're going to be the only one reading your code, just write it the way that fits best your style and make your life easier.
Would it also be possible to add a symphony parameter from the event? How would that be done? Setting something in $this->_env['param']['test'] doesn't seem to work.
Frontend::instance()->Page()->_param['test'] = 'Oh Hello!';
Great, thanks, I think that is an even nicer solution :)
Create an account or sign in to comment.
I have an event that creates an entry. The id of this entry should be given to one of my datasources. I've tried doing this using PHP and a global variable and a session variable, but it seems the data source is executed before the event is run.
Can I influence the execution order? Or is there a better solution?
Thanks in advance!