How to clone/copy a sql record with CakePhp? Is there a native way or do I need to find my record and then save it?
2 Answers
You need to use the find and save function.
$record = $this->Model->findById(1);
$record['Model']['id'] = NULL;
$this->Model->save($record);
There is no native "copy" command by itself. But a find/read operation followed by a create/save should work.
$row = $this->Model->findById(1);
$this->Model->create(); // Create a new record
$this->Model->save($row); // And save it
Would copy the row with id 1.
-
Unsetting the
$row['Model']['id']
would be useful, otherwise the same record is going to be overwritten by itself and the end result is that nothing changed. Commented Jan 15, 2020 at 10:44