Search

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!

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 :)

I've got the same need as frenkel, I have an event that creates an entry and I need the id of this entry available to one of my datasources. I'm a complete beginner at customizing symphony code (and php for that matter). My guess about calling Frontend::instance()->Page()->_param['xxx'] = 'yyy!';, as brendo suggested, is that this would happen in the event.xxx.php file?... just before calling trigger()? My situation has a twist that may or may not changes things (I'm pretty clueless about all this right now)... my page A is posting the form that triggers the event via AJAX to page B which produces as JSON version of the results which is returned to page A.

OK, I tried using Frontend::instance()->Page()->_param['xxx'] = 'yyy!'; in my event.xxx.php and I'm on the right track but I need to get to the system id of the entry created by this event. This would have to happen after trigger()'ing the event? But I'm not sure if the entry is accessible from the event... I haven't found the code or docs yet to answer that question. Can anyone give me a hint?

In the event you are customising you'll at the bottom of the __trigger() function a line that looks like return $result;. $result is an instance of the XMLElement class, so we can use this to get the entry ID (if it exists).

if($result->getAttribute('id')) {
    Frontend::instance()->Page()->_param['xxx'] = $result->getAttribute('id');
}

return $result;

Thanks brendo, that worked like a charm.

Oh. My. God, I've wanted to know how to do this for ages. Always put a really long winded way in place. Time to go change a load of code.

Wow this saved me a huge effort. Thanks!

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details