I'm having an issue using Transactions, here is some code example:
//Doing values insertion try { $transactionManager = new Phalcon\Mvc\Model\Transaction\Manager();
$transaction = $transactionManager->get();
$user = Users::findFirst("uid='".$userId."'");
if ($user == false) {
$user = new Users();
}
$user->setTransaction($transaction);
$user->uid = $userId;
$user->status = 'active';
$user->created = date('Y-m-d H:i:s');
$ipAddress = explode(',', $app->request->getClientAddress(true));
if ($user->save() == true) {
$toolbar = new Toolbars_Install();
$toolbar->setTransaction($transaction);
$toolbar->ip_address = $ipAddress[0];
$toolbar->created = date('Y-m-d H:i:s');
$toolbar->uid = $user->id;
if ($toolbar->save() == true) {
$user->toolbar_install_id = $toolbar->id;
$user->save();
} else {
$transaction->rollback('Unable to save model [toolbar], error: '.$errors);
}
} else {
$transaction->rollback('Unable to save model [user], error: '.$errors);
}
$transaction->commit();
} catch(Phalcon\Mvc\Model\Transaction\Failed $e) {
\Classes\Utils\Log::exception($app->config->logs->toolbars->install, $e);
} catch (Exception $e) {
\Classes\Utils\Log::exception($app->config->logs->toolbars->install, $e);
}
doing this I'm getting an exception
Line: 120, Code: 23000, Message: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2-c749b72fda4299b64363cab38dfb280d' for key 'PRIMARY'
Line 120 in my file is the line related to: $user->save();
Looking into the generated queries, Phalcon is generating a sencond INSERT for the Users model.
If I do:
$user->update(array( 'toolbar_install_id' => $toolbar->id ));
The update query is not getting generated.
I'm using Phalcon 0.8.0