Passing Data between Two Snippets VI

Passing data between two snippets using a custom database table

Variable values don't persist across different snippets, even when they're on the same page or in the same template. In the previous article, we looked at using User Settings to pass data to another snippet. In this one, we'll discuss how to do it with a custom database table.

MODX logo

Custom Database Tables

This will be short and sweet because the techniques involved have been covered on other pages at Bob's guides.

We'll look at two methods to store and retrieve data that can be passed between snippets. Both are preferable to the User Settings we saw in the previous article if you will have many users and a lot of data to save for each user.

If the Data is User-specific

If you need to save user-specific data for a logged-in user in one snippet and retrieve it in another, the easiest way is to extend the modUser object either on your own, or with the ClassExtender extra. There's a discussion here about why you might want to do this.

ClassExtender will create the custom table for you and will put the fields on the Create/Edit User panel for you. Retrieving the data will be fast, as will searching and sorting based on the stored data.

If the Data is *Not* User-specific

Sometimes, you want to pass data from one snippet to another that is not user-specific. You can do it with $_SESSION variables, cookies, or a file as discussed in previous articles, but if those don't meet your needs, a custom database table is usually the best option. Please don't be tempted to store the data in TVs. They are meant to hold resource-specific data. In other words, TVs are designed to hold data that will be used on a specific page. You can create a "data" page and stuff all your data into TVs there, but you shouldn't. It will be slow, and TVs are terrible for searching and sorting. Storing generic data there is a perverted use of TVs.

The creation and usage of custom database tables is covered in this article at Bob's Guides (and elsewhere if you Google MODX custom database tables). If you create your tables correctly, you can use all of the xPDO methods to deal with them (getObject(), getCollection(), fromArray(), toArray(), etc.). Doing so has many advantages such as the convenience of xPDO, and the fact that the data can be cached.

One last note, if the data is not user-specific, you can also store it in one or more chunks. Chunks are just hunks of text, so you can store data using $chunk->setContent(), $chunk->save(), and retrieve it with $modx->getChunk('chunkName'). If the data is in the form of an array, you can convert it to JSON using $modx->toJSON('arrayName'); and convert it back with $modx->fromJSON($string). You can also store the data in a specific format and parse it with PHP when you need it.

Obviously, using chunks to store your data is not a good choice if you need to search for specific data or sort results, or if there will be a lot of data stored. Chunks are a convenient method, though, for passing small pieces of data from one snippet to another. And using chunks will work fine if the two snippets are on the same page as long as they are in the correct order.

Coming Up

In the next article, we'll look at sending a user to their own specific page when they log in.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.