OllyBarca
OllyBarca

Reputation: 1531

How to clone/copy a sql record with CakePhp, and then grab the ID of the clone?

I have cloned/saved a copy of an SQL record using CakePhp like so:

$contract = $this->Contract->findById($id);
$contract['Contract']['id'] = NULL;
$this->Contract->save($contract); // And save it

I want to be able to redirect the user to the 'Edit' view of the cloned record after the clone is complete. Basically, I need to do something like this:

 $this->redirect(array('controller' => 'contracts', 'action' => 'edit', $cloneId));

My question is, how do I get the ID of the clone immediately after saving the new record?

Upvotes: 0

Views: 972

Answers (1)

Anil kumar
Anil kumar

Reputation: 4177

you can also get the last inserted record id in these ways.

$cloneId = $this->Contract->id;

or

$cloneId  = $this->Contract->getLastInsertID();

or

$contract = $this->Contract->save($contract);
$cloneId = $contract['Contract']['id']

Upvotes: 3

Related Questions