Chrishow
Chrishow

Reputation: 373

Two users editing same table/resource

I have a tool written in CakePHP which among other functionalities, given a list of products, allows me to create orders, purchase orders, product catalogs etc for my clients. However I have multiple users creating and editing these orders which brings me to the question.

How can I show a warning or somehow prevent more than one user to work on the same object? Right now everyone has to ask verbally before editing anything.

Thanks in advance.

Upvotes: 3

Views: 274

Answers (3)

Dariusz Majchrzak
Dariusz Majchrzak

Reputation: 1237

You can set flag in table when user requests for product and remove flag on save.. but theres a few additional problems:

  • what if user close browser,
  • what if user accidentally refresh page
  • what if battery dies in users laptop?
  • what if user accidentally dies? :) ...

My proposition is:

  • Add 'datetime' column to your table, lets call it "lock_date":

  • Add lock_user_id column to your table,

  • When user open product - set lock_date to current datetime, and lock_user_id from session

  • On client side: send ajax request to other method every minute for update "lock_date" field with current datetime

  • When ANY user tries to open the same product:

1) check if lock_date < current datetime minus 1 minute. If true: allow edit

2) If false: check if lock_user_id == session.user_id. If true - allow edit (this condition is useful if user accidentally refresh page, or try to open the same product when accidentally pressed "back" button, or trying to edit product in other browser)

Upvotes: 1

Rayann Nayran
Rayann Nayran

Reputation: 1135

You could add a user_id column to yours tables, once a user request to work with an Entity, you set this user_id to the entity and save it on database. It could be defined at the controller initialize() function, if the action requested match your condition.

And at beforeSave() function, you could set the user_id to null.

Then, before you render the template you check if user_id is null to render or not.

Upvotes: 1

danMontague
danMontague

Reputation: 71

You could set a flag on the object to that get's set to true when that object is requested for editing and false when the edit has been persisted to the object.

With that flag you could then prevent the object from being edited when

if(!flag) {
    //code with form to edit the object
    <form action="object.php" method="post">
      .....
    </form>
} else {
   //either disable the form fields or better solution show message to 
   //user that the object is being edited and they should try again later
   $this->flash->set("This record is currently being edited. Please try again later");
}

Upvotes: 0

Related Questions