How to copy values from one column to another using CakePHP 4 Query Builder

Written by James McDonald

July 9, 2020

I want to change the name of a column in my database and to do that I have to create a migration and drop a table

The query I want to run is

UPDATE pallets 
    production_date = print_date;

Seem so simple in SQL but to do it in CakePHP 4 took some finding

Copy data between columns in CakePHP 4

        $builder = $this->getQueryBuilder();
        $exp = $builder->newExpr('print_date');

        $statement = $builder->update('pallets')->set('production_date', $exp);


When you debug($statement) you get the following in the Query Object

'sql' => 'UPDATE pallets SET production_date = (print_date)'


Submit a Comment

Your email address will not be published.

You May Also Like…


Zebra Printer Language The above website has a page where you can enter ZPL and it...

PHP Iterators

Just came across a Youtube talk "Iterators in PHP" by Jake Smith published in 2014 that steps through the many...

PHP array_map Multiple Arrays

array_map can take multiple arrays. I like how it starts mapping through them starting at the first element of each...