1

I'm just starting out with CakePHP, and I can't find any support for implementing an optimistic locking scheme. The closest I could find was a comment on this CakePHP blog post saying that it wasn't supported in June 2008.

Does anyone know if that has changed, or if someone has published an extension or a tutorial on how to implement it yourself?

For a description of optimistic locking, see this answer.

Community
  • 1
  • 1
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286

3 Answers3

4

Not in the Core, and after a quick google it doesn appear that anyone is sharing a behavior if they made one. That would be my suggested tactic.

Alexander Morland
  • 6,356
  • 7
  • 32
  • 51
  • I suspected a behavior was the way to go, thanks for the confirmation. If I get one working, I'll post a link here. – Don Kirkby Apr 01 '09 at 16:32
2

In the book "Practical CakePHP Projects" on chapter 10 is some code given to make your own "Magic Fields". One of them is a field for optimistic locking. I haven't tried it yet, but it looks quite good actually.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • Thanks for the tip. I added a link to the book for others to follow. The book preview is actually missing the two pages from chapter 10 that talk about optimistic locking, but you can download the source code. I think I'll use some features like the way they use validation errors, but they seem to be updating the lock value on read, not on write. That seems wrong to me. – Don Kirkby Oct 27 '09 at 20:49
0

Tadashi Nakamura has posted an OptimisticLock behaviour on GitHub. It's not quite perfect, because it does the query for the last modified date before writing the changed record. That leaves you open to either silently overwriting another user's changes or hitting a deadlock. When I tried to write a behaviour like this, I wanted to include the last modified date in the update's where clause and fail if the rows affected is zero. At that time, there was no support for adding an extra condition to an update query. I haven't used PHP lately, so I don't know if that has changed.

Community
  • 1
  • 1
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286