Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 3 months ago.
0

Transactions should be helpful here:

http://laravel.com/docs/database#database-transactions

So if everything (including operation on remote server) goes right, your database will be updated, if not - all DB changes will be rolled back

Last updated 3 months ago.
0

@marekmurawski It's not a goal.

I want to avoid overwriting data by a second person. If I start inserting photo, and You start inserting photo before i saved changes, my changes will be overwrite by You because You read article before I saved it.

FR - First Request. SR - Second Request. Article content = Test.

  1. FR: Get article // Get: Test.
  2. FR: Make changes // Change to: Test. Made by FR.
  3. SR: Get article // Get: Test.
  4. FR: Save changes
  5. SR: Make changes
  6. SR: Save changes

In point 3 SR should get "Test. Made by FR.". So SR should wait until FR save their changes before he (SR) can read.

Last updated 3 months ago.
0

In this case you should make some custom locking mechanism. An idea would be to set article.locked_at and maybe locked_by_user_id timestamp properties at the moment when first user opens it for editing. You would need to check if article is locked before providing edit form and before storing changes. Of course the first user might start editing but not save changes at all - therefore the time-based locked_at - so the lock would expire within certain time of inactivity.

Another option would be to save revisions of articles in a separate table. This way any single edit would be accessible in history.

These are just free thoughts... maybe someone wiser will help you more :)

Last updated 3 months ago.
0

What about locking in database layer? I found this, but I don't know how can I use it: http://laravel.com/docs/queries#pessimistic-locking

Edit: In InnoDB engine i can use DB::transaction() and lockForUpdate(), but in articles table i need MyISAM for full-text search.

So, implementing custom lock mechanism as @marekmurawski said, is the only solution?

Last updated 3 months ago.
0

one common method of doing this is to manage a data-versioning protocol yourself. For example,

  • all your tables will need to contain a 'dataVersion' INT column,
  • whenever you fetch data from a record, also fetch the dataVersion column
  • whenever you execute a SQL UPDATE a record, increment the dataVersion column BUT add this to the WHERE clause: ' WHERE dataVersion = :myDataVersion'.

:myDataVersion is what came back when you loaded the record.

So, if user A reads a record, he will get a dataVersion of,say 5. So will user B. User A then updates the record and the record's dataVersion becomes 6.

Some time later, user B goes to update that record but this will not succeed because the WHERE clause will fail. He is checking for a dataVersion of 5 but the record now has 6.

The above works well where you have control of the SQL UPDATE statement. I have yet to figure out how to alter the UPDATE SQL generated by Laravel, so that it contains the above WHERE addition.

Last updated 3 months ago.
0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

kwolniak kwolniak kwolniak Joined 31 Jan 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.