1

I'm looking for a better way to update a set of taxonomy terms. It's currently causing cron to hang which slows down the site substantially.

    //Look through all of the stocks    
    foreach ($terms_data as $symbol => $data) {
        //Check to see if term exists in the default vocabulary
        $matching_terms = taxonomy_get_term_by_name($symbol, __DEFAULT_VOCAB_MACHINE_NAME);

        //Loop through existing term(s)
        foreach ($matching_terms as $term) {
            //Load the new vocab term
            $new_term_data = taxonomy_term_load($term->tid);
            foreach ($data as $k => $v) {
                //Add in the new data
                $field_name = substr("field_".$k, 0, 31);
                $new_term_data->{"$field_name"}['und'][0]['value'] = $v;    
            }
            taxonomy_term_save($new_term_data); 
        }
    } //endforeach  

The only way I could figure out how to get it to work was to:

  1. Find the term that matches the $symbol variable
  2. Loop through the matching term (1 term)
  3. Create a new term
  4. Update the exact fields
  5. Save overtop of the old term

I know there has to be a better way to do this. Any ideas? Thanks in advance!

  • I know this isn't particularly well documented which is why I am posting here. If anyone has any advice for myself or future devs please speak up :) – ryanka Nov 19 '14 at 16:49
  • I'm currently dealing with this exact issue. I'd love to hear how you ended up solving it. – Brent Connor Jul 17 '17 at 16:32
2

For those of you interested: the bottleneck is in the taxonomy_term_save() function.

Following the trail, it led me to the field_sql_storage module (in core) which deletes an existing row and then inserts it. It should really use the built in DB Driver functionality and UPSERT instead of deleting and then inserting.

Working issues (for D8 only):

As of writing this, no patches have been applied yet. Updating large sets of taxonomy terms will lock up your DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.