<commit_before>---
layout: page
title: "Using Transformations"
category: tra
date: 2016-10-18 15:14:46
---

The third part of Blueprint is the **Transformation** - a function which is called after the query output, and modifies it in some way. *TO-DO: add code for handling transformations before query execution*

    $this->addTransformation('convertTimes', function($record){
        if(isset($record->created)) {
            $record->created = date('Y-m-d', $record->created);
        }
        return $record;
    });
    
When applying a Transformation, Blueprint will pass the query output into the Transformation function. (If it receives an array, it will loop over the array and pass each element into the function.)

Transformations are best used in the form above - working on a single record, assumed to be a plain PHP object, with a check to make sure that the field it's operating on exists.<commit_msg>Added more info to the transformations page<commit_after>---
layout: page
title: "Using Transformations"
category: tra
date: 2016-10-18 15:14:46
---

The third part of Blueprint is the **Transformation** - a function which is called before or after the query, and modifies its input/output in some way.

    $this->addTransformation('convertTimes', function($record){
        if(isset($record->created)) {
            $record->created = date('Y-m-d', $record->created);
        }
        return $record;
    });
    
When applying a Transformation, Blueprint will pass the input or output into the Transformation function. (If it receives an array, it will loop over the array and pass each element into the function.)

Transformations are best used in the form above - working on a single record, assumed to be a plain PHP object, with a check to make sure that the field it's operating on exists.

When applied to an INSERT or UPDATE, the Transformation is performed on the input before the query is performed. When applied to a SELECT, the Transformation is applied to the query's output.
{'fraction_non_alphanumeric': 0.08163265306122448, 'fraction_numerical': 0.013937282229965157, 'mean_word_length': 3.7183098591549295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6194344', 'n_tokens_mistral': 532, 'n_tokens_neox': 497, 'n_words': 263}