CodeIgniter Forums

Full Version: CodeIgniter transaction, starting it and canceling it in different functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]WebMada[/eluser]
Hi

I want to emulate draft in my backoffice forms: in fact, there are 2 buttons in my forms, "Overview" and "Save". Overview does not commit changes but it serves for displaying draft of the website.

Is it possible to start the transaction in Overview button.
And commit with Save button.
Cancel transaction (lose modification) when opening form.

How to do: never commit if Save button not clicked?

For my curiosity, what is the difference between trans_begin() and trans_start()??

Webmada

El Forum

[eluser]WebMada[/eluser]
I have already detected a problem with the transaction to emulate draft!

The changes are already public but we can just rollback them. It's too bad for me! The principle of draft is that the modifications are not saved and are visible by only creators.

El Forum

[eluser]Otemu[/eluser]
Hi,

you could have something like this:
Code:
function overview(){
//overview transaction has begun
$this->db->trans_begin();
$this->db->query('AN SQL QUERY...');
}
function cancel(){
   //roll back transaction
    $this->db->trans_rollback();
}

function save(){
//success complete
$this->db->query('AN SQL QUERY...');
    $this->db->trans_commit();
}

The difference between trans_begin() and trans_start()
trans_begin() is manual, if a query fails you will need to check if it failed and then decide whether to rollback or do something else, with trans_start if a query fails it will automatically roll back.

I don't think you need to use transactions for this however, here is some solutions that think be useful:

Quote:1) Your preview script builds a form filled with hidden elements that is populated with the data your user just POSTed; the "Save" button would then submit that form to your processing script.
2) Use a second database table that mirrors your structure and use it for temporary storage, sort of a "Save but don't publish" kind of thing.
3) Add a column to your database that stores the status, i.e. use a TINYINT column and store a 0 if the user has not published yet or a 1 if the user has.

source quote here

In your case option 2 probably seems the best option

Quote:The principle of draft is that the modifications are not saved and are visible by only creators.

This way creators/admin can see the changes, however users will see the published version.

Alternatively Option 3 you could cache the saved db query and clear that cached result only on next save.

El Forum

[eluser]WebMada[/eluser]
Thanks for the precode about CI transaction but it is no more the solution as you said!

About the 3 propositions:
1) I don't understand this first

2) It is the solution that I've thought: second database or duplicates of the tables.
But I have also to duplicate all my models and some methods of controllers. And obviosly the views for preview!

3) That does'nt convine to me because it is not just about 1 table but 10 linked tables.