Add a field to multiple items at once based on tag

Is there a way - running a script on the back end would be fine - to update all items tagged with a particular tag to include default values in certain fields? For example, let’s say I have 500 items tagged with “abc” and for every item tagged “abc” I want the DC field “Coverage” for those items to be set to “Nationwide.” How would I do that quickly? At first I thought it would be something simple, such as:

UPDATE omeka_element_texts SET ‘text’ = ‘Nationwide’ WHERE element_id = 38 AND record_id IN (SELECT record_id FROM omeka_records_tags WHERE tag_id = 2086)

where element_id 38 is Coverage and tag_id 2086 is the tag ‘abc.’

But then I realized that this presumes there is already a record in omeka_element_texts with Coverage information filled out, when obviously there is not. … So I guess the answer is that I’d need to insert an entirely new record somehow?

I ran into this same problem and wrote the function below to solve it. It deals with the case where the element you want to update already has an element_texts record and the case where it does not. I derived it from the logic that Omeka uses when you save an item.

You’ll need to do a Select query to fetch the items matching the tag, then loop over the results, calling the function for each item.

    public static function updateElementText($item, $elementId, $text)
    {
        $element = $item->getElementById($elementId);

        // Remove the old value(s) for this element.
        $elementTexts = $item->getElementTextsByRecord($element);
        foreach ($elementTexts as $elementText)
        {
            $elementText->delete();
        }

        // Add the new value.
        if (strlen($text) != 0)
        {
            $item->addTextForElement($element, $text);
            $item->saveElementTexts();
        }
    }

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.