Dacobah
Dacobah

Reputation: 789

How to clone/copy a sql record with CakePhp?

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?

Upvotes: 6

Views: 5544

Answers (2)

Oldskool
Oldskool

Reputation: 34837

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.

Upvotes: 6

noslone
noslone

Reputation: 1289

You need to use the find and save function.

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

Upvotes: 18

Related Questions