How can i test a transaction?

Hello i created this topic because i have been trying to test my transactions with an id duplicate in my database, that does not have worked for me, and i don’t know if that’s a wrong way to test it or if transactions aren’t working, I’m using cakephp 2.2.5, it’s out of date but the app i’m working on is not mine so i can’t update it.
I hope someone can help me, thanks in advance.

Depends on what you mean by “test” and “transactions”, but it should probably work just fine. Share some code that shows the problem you’re having, if you want any more specific answer.

$this->MyModel->query("START TRANSACTION");//i have already used the transaction functions too

try{
foreach(){
//...data proccess
 $this->MyModel->save($myData);
 
 foreach(){
  //...some data process
  $this->MyModel->assocciatedModel->save($data);
 }
}



}catch(Exception){
 $success = false;
}

if($success){
 $this->MyModel->query("COMMIT");
}else{
 $this->MyModel->query("ROLLBACK");
}

I have code like this, im saving some data but my transaction isn’t working.

I wonder if something in Cake’s save function is doing its own transaction management and committing before control returns to you. I know that in Cake 3 there is code that tries to manage that and avoid starting a new transaction if one is already going on; don’t know what v2 has in that regard.

I have seen that is probably an error when code has an iterator, because I have tryed with a simple example using iterators (a for saving records) and it only saves the first record is processed on, but if I try to save 2 records or more (without an iterator, only using save method for all records) it saves all data and if an error appears no one record is saved.