How do I update all the resources?

I needed to add a new tv field with a drop-down list. When you open the resource and save to the resource, 1 option in the list is saved. But I have more than 2.000 resources and it takes too long to open all the resources manually and change them. How to do it quickly?

If you set a “Default Value” for your TV, you probably don’t need to save every resource.


Alternatively you could write a snippet and run it once to create the TV values. Something like this might work:

<?php
$allResources = $modx->getIterator('modResource',array('template' => 1));
foreach ($allResources as $r) {
    $r->setTVValue('myTV', 'the value');
}

Replace myTV with the name of your TV, 'the value' with the value that should be saved and change the number in array('template' => 1) to the id of your template.

That’s not an option.
If you set the default value, it does not appear when I output data from TV “[[#[[+tv.item_brand]].pagetitle:ucase]]”, it appears only if I open the resource and click “Save”.
I need exactly to repeat the action of opening and saving resources automatically, because it takes too long to do it 2000 times.

what about the second option with the snippet, @halftrainedharry is talking about?
I think, that should do, what you need.

There are about 3,000 resources in total. The 1000 resources already have different “options” on the drop-down list. For the remaining 2,000, you need to “select” the first item in the drop-down list. If I use the second option, I will overwrite all the resources, even those that don’t need to be touched.
Therefore, I repeat, I need to repeat the “open and save” action of the resource automatically.

In the snippet, you can check for allready setted values and skip that resources. So it would only set the value, where it is needed.

That’s why I asked for help here.

Is this a list with fixed values or are the values different for every resource?


In the snippet you can read the current value of the TV with the function getTVValue().

$currentTvValue = $r->getTVValue('myTV');

Is this a list with fixed values or are the values different for every resource?
Different for different resources.
You only need to select 1 in the list for items that don’t have anything selected yet.

Maybe you can achieve what you want with an SQL statement.

Something like this may work:

INSERT INTO modx_site_tmplvar_contentvalues(`tmplvarid`, `contentid`, `value`)
SELECT 99, id, 'the value'
FROM modx_site_content SC 
WHERE template = 1
AND NOT EXISTS (
    SELECT * 
    FROM modx_site_tmplvar_contentvalues 
    WHERE contentid = SC.id AND tmplvarid = 99
)

Replace 99 with the actual ID or your TV (in both places), 'the value' with the value of your TV and template = 1 with the actual ID of your template

Make a backup of your database before you try this!

I wonder if the CustomSearch extra would work for you. It allows you to do search-and-replace on TV values, with the option to do a dry run that shows what it would have done.

It also allows regular expression search-and-replace.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.