Two users editing the same table / resource

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

How can I show a warning or somehow prevent multiple users from working on the same object? Everyone should now verbally ask questions before editing.

Thanks in advance.

+3


source to share


3 answers


You can set a flag on the table when the user asks for a product and removes the flag on save .. but there are a few additional problems:

  • what to do if the user closes the browser,
  • what if the user accidentally refreshes the page
  • what if the battery dies in users laptop?
  • what if the user accidentally dies? :) ...

My suggestion:



  • Add a column "datetime" to your table, let's call it "lock_date":

  • Add lock_user_id column to your table,

  • When the user opens the product - set lock_date to the current time and time, and lock_user_id from the session

  • Client side: send ajax request to another method every minute to update the "lock_date" field with the current lifetime

  • When a user tries to open the same product:

1) check if lock_date is <current time of day minus 1 minute. If true: enable editing

2) If false: check lock_user_id == session.user_id. If true - allow editing (this condition is useful if the user accidentally refreshes the page or tries to open the same product by accidentally clicking the back button or tries to edit the product in another browser)

+1


source


You can add a column user_id

to your tables, after the user has requested to work with an entity, you will set this one user_id

for the object and save it to the database. It can be defined in a controller function initialize()

if the requested action matches your condition.

And in the function, beforeSave()

you can set user_id

to null

.



Then, before rendering the template, you check if there is one user_id

null

to render or not.

+1


source


You can set an object flag to be true if the object is requested for editing, and false when the edit is saved to the object.

With this flag you can prevent editing of an object 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");
}

      

0


source







All Articles