Using getValue() with TVs in MODX

In a previous article, we looked at a very fast and efficient xPDO method for getting the value of a single field from a MODX object using the getValue method of the xPDO object. For example, this code will get the introtext field from a resource with the pagetitle “Products”:

[code language=”php”]
$query = $modx->newQuery(‘modResource’, array(
‘pagetitle’ => ‘Products’,
));
$query->select(‘introtext’);
$intro = $modx->getValue($query->prepare());
[/code]

In this article, we’ll look at using the method to get TV values for specific resources. This is much trickier because TV’s set to their default values require an extra step.

MODX logo

TVs are always tricky to deal with because their values may be stored in more than one place and they are never stored in the table that contains the resource.

Traditionally, you get the value of a TV for a particular resource with one of these methods:

[code language=”php”]
/* When you have the TV object and want
the raw value of the TV */
$val = $tvObj->getValue($resourceId);

/* When you have the TV object and want
the processed value of the TV */
$val = $tvObj->renderOutput($resourceId);

/* When you have the resource object and want
the processed value of the TV */

$val = $resource->getTVValue($tvId);
[/code]

But what if you don’t have either the TV or the Resource Object? In that case, the methods above are quite wasteful and slow because they require you to get one of the objects first, when all you really need is a particular field value (the field containing the value of the TV for a single resource).

We can use our method here to speed things up a tremendously, but it takes an extra step. Most TV values are stored in the modTemplateVarResource object (in the modx_site_tmplvar_contentvalues table). The catch is that if the TV is set to its default value, there is no record in the table for it. The methods above know that if there’s no value in the table, they have to consult the TV object itself to get the default value. We have to do the same.

In the following example, we want the value of TV 12 for Resource 23:

The Code

[code language=”php”]
$resourceId = 23;
$tvId = 12;

/* Get the value of the modTemplateVarResource if possible */
$query = $modx->newQuery(‘modTemplateVarResource’, array(
‘contentid’ => $resourceId,
‘tmplvarid’ => $tvId,
));
$query->select(‘value’);
$val = $modx->getValue($query->prepare());

/* Get default value of the TV if there’s
no record and the query fails */
if ($val === false) {
$query2 = $modx->newQuery(‘modTemplateVar’, array(
‘id’ => $tvId,
));
$query2->select(‘default_text’);
$val = $modx->getValue($query2->prepare());
}
[/code]

Notice that the array in our query has to be a little more complex. This is because we need to specify both the TV’s ID and the resource’s ID to get the correct modTemplateVarResource object.

It’s also worth noting that finding the TV values will be faster if there is no default value set for the TV, or if the default value is not used for any resource. That way, the TV values for all resources will be in the modTemplateVarResource table. This is true for the traditional methods as well.

Caveats

Remember that this method will always get the raw value of the TV as a string, not its processed value. If you use it on an Image TV, for example, you won’t get the full image tag, just the path to the image. For date TVs, you’ll get the raw timestamp as a string. For almost any TV, though, you can use the raw value if you know what it is and what you want to do with it. Doing so will be significantly faster.

This method will not process @ bindings like @INHERIT at all. If you have a TV with an @ binding, use one of the traditional methods for getting the processed value of the TV, which will handle them correctly.

Using @ bindings can be very convenient, but they come with a price. For @INHERIT, for example, if a TV value for a resources is not set, and the TV’s default value is @INHERIT, MODX first has to get the default value of the TV. Then it has to walk up the tree, checking the TV value of each resource, until it finds a resource with an actual value in the TV. That takes time.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *