Single Edit ("Row Lock") - just hook and js nothing else!
Posted: 2022-07-20 11:22
Have been looking for an easy way to do this without anything fancy.
We all know the problem of double editing, row locking, deadlocks etc.
Finally got this idea, works perfect for what I need, sure will piss off some users, but I do not have that many users using the app at the same time (happens but very little).
If you have a busy app, you need to look else where for sure!
I had two fields that I wanted for something else, but I decided to use them since I already had them in every table (if you are creating from scratch choose better names for the fields)
Enough talk, let´s do business:
In your table you need two fields (owner, group) (Choose better names, like edited and lock or something)
owner field should have automatic <%%editingTimestamp%%> (read only and HIDDEN)
group just VarChar 40 (normal no read only and not hidden)
open this file, create it if it does not exist:
tableName-dv.js and add the following lines
The color should match your theme to make the field vanish, the user should not see this.
Now for the hook file of the same table:
go to function after insert and add the following
go to function before update and add the following
Appgini will retrieve the new data automatically, you need to warn your user that the new data is shown now before he hits save again.
if you click save again after the message, your editing will be saved.
the other guy that saved the last editing will get the error message if he tries to save a second time, and so on.
Works perfect for me without locking, time lock etc.
I hope this solves the problem for some of you.
Please advise if there is a better approach while keeping it simple.
What I am trying to do now is: instead of saying Someone in the error message I want to put the user name instead.
No luck so far, so I added a field showing edited by! does the same but would be better if I put it in the message.
I am sending this to Ahmed appgini for his evaluation!
Thank you and happy coding
We all know the problem of double editing, row locking, deadlocks etc.
Finally got this idea, works perfect for what I need, sure will piss off some users, but I do not have that many users using the app at the same time (happens but very little).
If you have a busy app, you need to look else where for sure!
I had two fields that I wanted for something else, but I decided to use them since I already had them in every table (if you are creating from scratch choose better names for the fields)
Enough talk, let´s do business:
In your table you need two fields (owner, group) (Choose better names, like edited and lock or something)
owner field should have automatic <%%editingTimestamp%%> (read only and HIDDEN)
group just VarChar 40 (normal no read only and not hidden)
open this file, create it if it does not exist:
tableName-dv.js and add the following lines
Code: Select all
document.getElementById("group").style.backgroundColor = "#2E3338";
document.getElementById("group").style.borderColor = "#2E3338";
document.getElementById("group").readOnly = true;
Now for the hook file of the same table:
go to function after insert and add the following
Code: Select all
// group = time() for the lock
sql("update `tableName` set `group`= UNIX_TIMESTAMP(), `owner` = `group` where `id` = {$data['id']}",$eo);
go to function before update and add the following
Code: Select all
// For the lock
$LockTime=sqlValue("select `owner` from tableName where `id` ='{$data['selectedID']}'");
if ( $data['group'] !== $LockTime){$args['error_message'] = 'Someone modified your row, check the new data or some other message!';return FALSE;} ;
$data['group'] = $data['owner'];
//===============================
if you click save again after the message, your editing will be saved.
the other guy that saved the last editing will get the error message if he tries to save a second time, and so on.
Works perfect for me without locking, time lock etc.
I hope this solves the problem for some of you.
Please advise if there is a better approach while keeping it simple.
What I am trying to do now is: instead of saying Someone in the error message I want to put the user name instead.
No luck so far, so I added a field showing edited by! does the same but would be better if I put it in the message.
I am sending this to Ahmed appgini for his evaluation!
Thank you and happy coding