We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38669
    • 51 Posts
    Hey all,

    Just getting into xPDO with modX. I have a template that includes a snippet where I add my package and get a record from the database. I can display the record. So far so good.

    Later in the page I want to use a second snippet to display another field from the record. So it looks like this
    [[!connect]]
    <some HTML>
    [[!display_more_data]]
    <more HTML>


    In my second snippet I have identical debug code as the first one (which works)
    echo " song = " .  $song->get('songArtist');exit;
    But it fails with Fatal error: Call to a member function get() on a non-object in

    Do I need to do the getObject() in each snippet? It looks like it but I want to be sure. It seems like that would be a waste of resources, or is the object already created and I am just pulling it in again?

    Hope that is clear. Thanks in advance
      • 22303 MODX Staff
      • 10,725 Posts
      Snippets are essentially executed in MODX as global functions and are isolated to their own scope. My recommendation is to separate your Snippets into those that retrieve data and store it for other Snippets to make use of. Typically, I will load data like this in a Snippet executed before anything else on a Resource and have it stored as a placeholder. Then, I can retrieve this data object (or array, or whatever) in any number of additional Snippets tasked with rendering specific output using that data.
        • 38669
        • 51 Posts
        Quote from: opengeek at Jan 26, 2013, 07:45 AM
        Snippets are essentially executed in MODX as global functions and are isolated to their own scope. My recommendation is to separate your Snippets into those that retrieve data and store it for other Snippets to make use of. Typically, I will load data like this in a Snippet executed before anything else on a Resource and have it stored as a placeholder. Then, I can retrieve this data object (or array, or whatever) in any number of additional Snippets tasked with rendering specific output using that data.

        Great - thanks opengeek. Never thought of placeholders. That will work shocked)

        Curious though, does using getObject() in each snippet consume resources? It seems like the object already exists so it is just retrieving it. Is it sloppy programming or is it OK?
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: xxxmicrobexxx at Jan 26, 2013, 05:20 PM

          Curious though, does using getObject() in each snippet consume resources? It seems like the object already exists so it is just retrieving it. Is it sloppy programming or is it OK?
          Yes, it does attempt to issue a query. There is not a built in global object cache in the xPDO core that works per REQUEST, though there is a database result set cache that extends beyond the REQUEST scope. But you can easily use placeholders as a REQUEST scoped cache for your objects, as I am suggesting here.