6

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 2

18

You need to use the find and save function.

$record = $this->Model->findById(1);
$record['Model']['id'] = NULL;
$this->Model->save($record);
6

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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