Monday, 14 April 2014

Detecting When Values Are Changed

Xataface provides hooks that allow you to respond to many events in the workflow of your application.  The most fundamental hooks involve saving records.  These include:

  • beforeSave - called just before a record is saved to the database.
  • afterSave - called just after a record is saved to the database.
  • beforeInsert - called just before a record is inserted into the database.
  • afterInsert - called just after a record is inserted into the database.
  • beforeUpdate - called just before an existing record is updated in the database.
  • afterUpdate - called just after an existing record is updated in the database.
Notice that before/afterSave is called every time a record is saved (i.e. for both updates and inserts).  One common requirement is to perform an action only if a particular value was changed in the record.  For example, suppose you have a "status" field in your table, and if this field is set to "broken", an email should be sent to the site administrator to "fix" whatever is broken.  A first attempt at solving this problem might be:
This will send an email to the admin (assume that ADMIN_EMAIL is a constant defined elsewhere) every time a record is saved and the 'status' is set to 'broken'.  But there is a major problem with this solution.  This will send an email *every* time the record is saved.  So if the record was set to broken, then a user went in to make additional changes, it would send another email after each save.  Ideally we want to just send the email when the value of 'status' first changed to 'broken'.

Detecting Field Value Changes

Dataface_Record has a valueChanged() method that makes it easy to detect if a field has been modified since the record was last loaded.  With this knowledge, you might try to modify your afterSave() method as follows:
Unfortunately, if you try to run this code, you'll find that the mail is now *never* sent. This is because the "changed" flags on Dataface_Record objects are reset before any of the afterXXX() methods are called. If you want to detect a change, you need to do so in the beforeXXX() methods of the save cycle. E.g.
Now if you try out the application it should work the way that you expect. If the value of the 'status' field is changed, an email will be sent as expected. But this still isn't ideal because the email is technically sent before the "save" cycle is finished. So there is a possibility that a field validator causes the save to be canceled or fail. That may seem like a slim possibility, but it is better to be sure.
So, our ideal solution is to use the afterSave() hook, but to only send the email if the status field has changed.

Passing Data from beforeSave to afterSave Callback

The solution, then, is to use the beforeSave() handler to detect the change, and then somehow tell the afterSave() callback to perform the update in the case that a change is found.  There are many ways to pass data between these methods, but my favourite solution is to use the "pouch" of the Dataface_Record object.
The "pouch" is just an associative array that is a member of a Dataface_Record object.  There you can store arbitrary data to be passed around and between contexts of your application.   In the following example, we use the beforeSave() handler to detect the change in the 'status' field.  We then register a flag in the record's pouch which will be detected in the afterSave() method.
This version works exactly as required. Notice that after detecting the 'send_broken_email' flag in the afterSave() method, it clears this flag so that it doesn't affect future cycles, in case the same record object is saved multiple times during the same HTTP request.

Using PHP Anonymous Functions


The previous version works fine, but as a matter of style, I prefer to use PHP's anonymous functions (available since 5.3) to define callbacks in the beforeSave() handler which can be called in the afterSave() handler.

The thing I like about this style is that all of the business logic will then reside in the beforeSave() method. If I create additional rules that require some action in the afterSave() method, I can just add more callbacks. The afterSave() method doesn't need to change.. it just faithfully executes the list of handlers. This could even be extended to add callbacks from other areas of your app. But I'll leave that for another post.

17 comments:

  1. Interesting... could you use this to create an 'Audit Trail'?
    A simple comparison between the the 'beforeUpdate' and 'afterUpdate' data should enable you to flag any changed data and store it into a log file or table

    ReplyDelete
    Replies
    1. Yes you could implement such a thing using this method. You may also want to check out xataface's history feature as it does something similar. http://xataface.com/documentation/how-to/history-howto

      Delete
    2. Get your ex lover back urgently Dr Larry email: assurancesulotionhome@gmail com call him on or whatsapp +2348102316854 immediately
      Life without my husband was a real  mess for me and my children I am so happy to get my Ex back through the  help of Dr Larry the spell caster . My greatest surprise was that 48 hours after the Doctor prepared the spell for me, my husband who has abandoned me for 4 years suddenly called me unexpectedly and am so happy that we have come to become one again through the help of Dr Larry and am so happy to be with my husband once again. Dr Larry is a very wonderful spell caster, you can contact him if you need his assistant because i know he can also help you. contact him through his email: assurancesolutionhome@gmail com call him on or whatsapp +2348102316854 immediately website  http://assurancesolutionhome.website2.me/



      Get your ex lover back urgently Dr Larry email: assurancesulotionhome@gmail com call him on or whatsapp +2348102316854 immediately
      Life without my husband was a real  mess for me and my children I am so happy to get my Ex back through the  help of Dr Larry the spell caster . My greatest surprise was that 48 hours after the Doctor prepared the spell for me, my husband who has abandoned me for 4 years suddenly called me unexpectedly and am so happy that we have come to become one again through the help of Dr Larry and am so happy to be with my husband once again. Dr Larry is a very wonderful spell caster, you can contact him if you need his assistant because i know he can also help you. contact him through his email: assurancesolutionhome@gmail com call him on or whatsapp +2348102316854 immediately website  http://assurancesolutionhome.website2.me/




      Get your ex lover back urgently Dr Larry email: assurancesulotionhome@gmail com call him on or whatsapp +2348102316854 immediately
      Life without my husband was a real  mess for me and my children I am so happy to get my Ex back through the  help of Dr Larry the spell caster . My greatest surprise was that 48 hours after the Doctor prepared the spell for me, my husband who has abandoned me for 4 years suddenly called me unexpectedly and am so happy that we have come to become one again through the help of Dr Larry and am so happy to be with my husband once again. Dr Larry is a very wonderful spell caster, you can contact him if you need his assistant because i know he can also help you. contact him through his email: assurancesolutionhome@gmail com call him on or whatsapp +2348102316854 immediately website  http://assurancesolutionhome.website2.me/

      Delete
    3. URGENT AND EFFECTIVE LOVE SPELL CASTER TO HELP YOU GET BACK YOUR EX LOVER VERY FAST 2019 Contact DR LARRY assurancesolutionhome@gmail. com WhatsApp  +2348102316854 I am Laura Peggy  from (ALABAMA), When I eventually found testimonies about this spell caster Dr Larry, how he helped many people to get their lovers and broken homes back, i contacted him too because I was absolutely desperate to get my lover back. Life without my lover was a real mess for me and my kids. i wanted a dramatic change and I thought love spell could be the solution. After discussing the resolution with Dr Larry, he gave me hope that he will restore my relationship. I felt confident that he will actually make my lover to return home and he did! It’s fantastic what this great spell caster have done for me, his help is priceless! I don't know what I would have done without Dr Larry, He does his job so well he is organized and highly functional, i believe he is the best spell caster i can count on when it comes to all kinds of spell, I am so happy that my lover return back home. If you need help, contact him right now through these details below: Email: assurancesolutionhome@gmail. com WhatsApp +2348102316854 Website: http://assurancesolutionhome.wordpress.com  http://assurancesolutionhome.website2.me/





      URGENT AND EFFECTIVE LOVE SPELL CASTER TO HELP YOU GET BACK YOUR EX LOVER VERY FAST 2019 Contact DR LARRY assurancesolutionhome@gmail. com WhatsApp  +2348102316854 I am Laura Peggy  from (ALABAMA), When I eventually found testimonies about this spell caster Dr Larry, how he helped many people to get their lovers and broken homes back, i contacted him too because I was absolutely desperate to get my lover back. Life without my lover was a real mess for me and my kids. i wanted a dramatic change and I thought love spell could be the solution. After discussing the resolution with Dr Larry, he gave me hope that he will restore my relationship. I felt confident that he will actually make my lover to return home and he did! It’s fantastic what this great spell caster have done for me, his help is priceless! I don't know what I would have done without Dr Larry, He does his job so well he is organized and highly functional, i believe he is the best spell caster i can count on when it comes to all kinds of spell, I am so happy that my lover return back home. If you need help, contact him right now through these details below: Email: assurancesolutionhome@gmail. com WhatsApp +2348102316854 Website: http://assurancesolutionhome.wordpress.com  http://assurancesolutionhome.website2.me/

      Delete
    4. My husband and I have been married for about 7 yrs now. We were happily married with two kids, a boy and a girl. 3 months ago, I started to notice some strange behavior from him and a few weeks later I found out that my husband is seeing someone. He started coming home late from work, he hardly care about me or the kids anymore, Sometimes he goes out and doesn't even come back home for about 2-3 days. I did all I could to rectify this problem but all to no avail. I became very worried and needed help. As I was browsing through the Internet one day, I came across a website that suggested that Dr Great can help solve marital problems, restore broken relationships and so on. So, I felt I should give him a try. I contacted him and he did a spell for me. Two days later, my husband came to me and apologized for the wrongs he did and promise never to do it again. Ever since then, everything has returned back to normal. I and my family are living together happily again.. All thanks to Dr Great . If you need a spell caster that can cast a spell that truly works, I suggest you contact him. He will not disappoint you. This is his E-mail: 

      infinitylovespell@gmail.com or infinitylovespell@yahoo.com

      WhatsApp +2348118829899

      website http://infinitylovespell.website2.me/

      Blog http://infinitylovespell1.blogspot.com






      My husband and I have been married for about 7 yrs now. We were happily married with two kids, a boy and a girl. 3 months ago, I started to notice some strange behavior from him and a few weeks later I found out that my husband is seeing someone. He started coming home late from work, he hardly care about me or the kids anymore, Sometimes he goes out and doesn't even come back home for about 2-3 days. I did all I could to rectify this problem but all to no avail. I became very worried and needed help. As I was browsing through the Internet one day, I came across a website that suggested that Dr Great can help solve marital problems, restore broken relationships and so on. So, I felt I should give him a try. I contacted him and he did a spell for me. Two days later, my husband came to me and apologized for the wrongs he did and promise never to do it again. Ever since then, everything has returned back to normal. I and my family are living together happily again.. All thanks to Dr Great . If you need a spell caster that can cast a spell that truly works, I suggest you contact him. He will not disappoint you. This is his E-mail: 

      infinitylovespell@gmail.com or infinitylovespell@yahoo.com

      WhatsApp +2348118829899

      website http://infinitylovespell.website2.me/

      Blog http://infinitylovespell1.blogspot.com







      My husband and I have been married for about 7 yrs now. We were happily married with two kids, a boy and a girl. 3 months ago, I started to notice some strange behavior from him and a few weeks later I found out that my husband is seeing someone. He started coming home late from work, he hardly care about me or the kids anymore, Sometimes he goes out and doesn't even come back home for about 2-3 days. I did all I could to rectify this problem but all to no avail. I became very worried and needed help. As I was browsing through the Internet one day, I came across a website that suggested that Dr Great can help solve marital problems, restore broken relationships and so on. So, I felt I should give him a try. I contacted him and he did a spell for me. Two days later, my husband came to me and apologized for the wrongs he did and promise never to do it again. Ever since then, everything has returned back to normal. I and my family are living together happily again.. All thanks to Dr Great . If you need a spell caster that can cast a spell that truly works, I suggest you contact him. He will not disappoint you. This is his E-mail: 

      infinitylovespell@gmail.com or infinitylovespell@yahoo.com

      WhatsApp +2348118829899

      website http://infinitylovespell.website2.me/

      Blog http://infinitylovespell1.blogspot.com

      Delete
  2. Thank you for a really useful blog entry. One gotcha that I fell down that is probably obvious to others but...don't change and save fields in the same record in the afterSave method - endless recursion ensues.....

    ReplyDelete
  3. QUANTUM BINARY SIGNALS

    Professional trading signals sent to your cell phone every day.

    Start following our signals right now and gain up to 270% per day.

    ReplyDelete
  4. "Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.php jobs in hyderabad.
    "

    ReplyDelete
  5. GPS robot tripled the $100k deposit [live proof]

    I just finished a webinar with Mark and his partner, Antony, two days ago and it was GREAT.

    During the webinar MARK and ANTONY shared their stories to success and answered questions about their new release of the GPS Forex Robot that CAME OUT TODAY!

    ReplyDelete
  6. Forex Trendy is a state of the art software capable of determining the most reliable continuation chart patterns. It scans through all the charts, on all time frames and analyzes every prospective breakout.

    ReplyDelete
  7. Get your ex lover back urgently Dr Larry email: assurancesulotionhome@gmail com call him on or whatsapp +2348102316854 immediately
    Life without my husband was a real  mess for me and my children I am so happy to get my Ex back through the  help of Dr Larry the spell caster . My greatest surprise was that 48 hours after the Doctor prepared the spell for me, my husband who has abandoned me for 4 years suddenly called me unexpectedly and am so happy that we have come to become one again through the help of Dr Larry and am so happy to be with my husband once again. Dr Larry is a very wonderful spell caster, you can contact him if you need his assistant because i know he can also help you. contact him through his email: assurancesolutionhome@gmail com call him on or whatsapp +2348102316854 immediately website  http://assurancesolutionhome.website2.me/



    Get your ex lover back urgently Dr Larry email: assurancesulotionhome@gmail com call him on or whatsapp +2348102316854 immediately
    Life without my husband was a real  mess for me and my children I am so happy to get my Ex back through the  help of Dr Larry the spell caster . My greatest surprise was that 48 hours after the Doctor prepared the spell for me, my husband who has abandoned me for 4 years suddenly called me unexpectedly and am so happy that we have come to become one again through the help of Dr Larry and am so happy to be with my husband once again. Dr Larry is a very wonderful spell caster, you can contact him if you need his assistant because i know he can also help you. contact him through his email: assurancesolutionhome@gmail com call him on or whatsapp +2348102316854 immediately website  http://assurancesolutionhome.website2.me/




    Get your ex lover back urgently Dr Larry email: assurancesulotionhome@gmail com call him on or whatsapp +2348102316854 immediately
    Life without my husband was a real  mess for me and my children I am so happy to get my Ex back through the  help of Dr Larry the spell caster . My greatest surprise was that 48 hours after the Doctor prepared the spell for me, my husband who has abandoned me for 4 years suddenly called me unexpectedly and am so happy that we have come to become one again through the help of Dr Larry and am so happy to be with my husband once again. Dr Larry is a very wonderful spell caster, you can contact him if you need his assistant because i know he can also help you. contact him through his email: assurancesolutionhome@gmail com call him on or whatsapp +2348102316854 immediately website  http://assurancesolutionhome.website2.me/

    ReplyDelete
  8. cheap ray bans sunglasses uk sale, combining elegant style and cutting-edge technology, a variety of styles of replica ray ban clubmaster, the pointer walks between your exclusive taste style.

    ReplyDelete
  9. URGENT AND EFFECTIVE LOVE SPELL CASTER TO HELP YOU GET BACK YOUR EX LOVER VERY FAST 2019 Contact DR LARRY assurancesolutionhome@gmail. com WhatsApp  +2348102316854 I am Laura Peggy  from (ALABAMA), When I eventually found testimonies about this spell caster Dr Larry, how he helped many people to get their lovers and broken homes back, i contacted him too because I was absolutely desperate to get my lover back. Life without my lover was a real mess for me and my kids. i wanted a dramatic change and I thought love spell could be the solution. After discussing the resolution with Dr Larry, he gave me hope that he will restore my relationship. I felt confident that he will actually make my lover to return home and he did! It’s fantastic what this great spell caster have done for me, his help is priceless! I don't know what I would have done without Dr Larry, He does his job so well he is organized and highly functional, i believe he is the best spell caster i can count on when it comes to all kinds of spell, I am so happy that my lover return back home. If you need help, contact him right now through these details below: Email: assurancesolutionhome@gmail. com WhatsApp +2348102316854 Website: http://assurancesolutionhome.wordpress.com  http://assurancesolutionhome.website2.me/





    URGENT AND EFFECTIVE LOVE SPELL CASTER TO HELP YOU GET BACK YOUR EX LOVER VERY FAST 2019 Contact DR LARRY assurancesolutionhome@gmail. com WhatsApp  +2348102316854 I am Laura Peggy  from (ALABAMA), When I eventually found testimonies about this spell caster Dr Larry, how he helped many people to get their lovers and broken homes back, i contacted him too because I was absolutely desperate to get my lover back. Life without my lover was a real mess for me and my kids. i wanted a dramatic change and I thought love spell could be the solution. After discussing the resolution with Dr Larry, he gave me hope that he will restore my relationship. I felt confident that he will actually make my lover to return home and he did! It’s fantastic what this great spell caster have done for me, his help is priceless! I don't know what I would have done without Dr Larry, He does his job so well he is organized and highly functional, i believe he is the best spell caster i can count on when it comes to all kinds of spell, I am so happy that my lover return back home. If you need help, contact him right now through these details below: Email: assurancesolutionhome@gmail. com WhatsApp +2348102316854 Website: http://assurancesolutionhome.wordpress.com  http://assurancesolutionhome.website2.me/

    ReplyDelete
  10. My husband and I have been married for about 7 yrs now. We were happily married with two kids, a boy and a girl. 3 months ago, I started to notice some strange behavior from him and a few weeks later I found out that my husband is seeing someone. He started coming home late from work, he hardly care about me or the kids anymore, Sometimes he goes out and doesn't even come back home for about 2-3 days. I did all I could to rectify this problem but all to no avail. I became very worried and needed help. As I was browsing through the Internet one day, I came across a website that suggested that Dr Great can help solve marital problems, restore broken relationships and so on. So, I felt I should give him a try. I contacted him and he did a spell for me. Two days later, my husband came to me and apologized for the wrongs he did and promise never to do it again. Ever since then, everything has returned back to normal. I and my family are living together happily again.. All thanks to Dr Great . If you need a spell caster that can cast a spell that truly works, I suggest you contact him. He will not disappoint you. This is his E-mail: 

    infinitylovespell@gmail.com or infinitylovespell@yahoo.com

    WhatsApp +2348118829899

    website http://infinitylovespell.website2.me/

    Blog http://infinitylovespell1.blogspot.com

    ReplyDelete
  11. My husband and I have been married for about 7 yrs now. We were happily married with two kids, a boy and a girl. 3 months ago, I started to notice some strange behavior from him and a few weeks later I found out that my husband is seeing someone. He started coming home late from work, he hardly care about me or the kids anymore, Sometimes he goes out and doesn't even come back home for about 2-3 days. I did all I could to rectify this problem but all to no avail. I became very worried and needed help. As I was browsing through the Internet one day, I came across a website that suggested that Dr Great can help solve marital problems, restore broken relationships and so on. So, I felt I should give him a try. I contacted him and he did a spell for me. Two days later, my husband came to me and apologized for the wrongs he did and promise never to do it again. Ever since then, everything has returned back to normal. I and my family are living together happily again.. All thanks to Dr Great . If you need a spell caster that can cast a spell that truly works, I suggest you contact him. He will not disappoint you. This is his E-mail: 

    infinitylovespell@gmail.com or infinitylovespell@yahoo.com

    WhatsApp +2348118829899

    website http://infinitylovespell.website2.me/

    Blog http://infinitylovespell1.blogspot.com

    ReplyDelete