2 reactor questions

1 view
Skip to first unread message

Gareth Cole

unread,
Sep 1, 2009, 1:11:16 PM9/1/09
to reacto...@googlegroups.com

Hi Folks,

 

I've a couple of questions about the best way to handle certain scenarios:

 

  1. I've got a Rota record. To this record, I've added a custom function called updateShifts(). I want this function to be executed every time the record is saved, after the save has been done. I thought about over-writing the save() method, calling super.save() and then updateShifts(). However, I also saw an afterSave() method that may be more appropriate to over-write. afterSave() already seems to have a lot of code in it, so I wasn't sure if it's safe to over-write. What's the recommended way of doing this?
  2. I'd like to trigger another custom function if one record property is changed and then saved. As an example, my User record has various fields, one of which is 'name'. If the name is changed and saved, a custom function in the record called getNewPassport() should be executed. Again, I suspect over-writing afterSave() may be the answer, but I've no idea how to monitor the field to see if it changed. Any thoughts?

 

Thanks,

 

Gareth

Dave Phipps

unread,
Sep 2, 2009, 4:52:49 AM9/2/09
to reacto...@googlegroups.com
I'm sure you'll get some different answers but I always look for the
simplest solution.

For number 1: presuming you are using some kind of service cfc to
create your record object and then run the save() method. I would
simply call your updateShifts() function after the save() call. e.g
(pseudo code):

function saveRota
argument name='rotaid'
var rotaRec = createRecord('Rota').load(rotaid=arguments.rotaid)
rotaRec.setrotaname('nameofrota')
rotaRec.save()
updateShifts()
return rotaRec.getrotaid()

For number 2: I would do something similar to the above. Presuming you
have a save function, e.g. saveUser and 'name' was one of the
arguments passed then simply do this:

function saveUser
argument name='userid'
argument name='name'
var userRec = createRecord('user').load(userid=arguments.userid)
if userRec.getname() neq arguments.name
//set a flag
set namechanged = true
else
set namechanged = false
/if
userRec.save()
if namechanged
getNewPassport()


If you need something more complex then it may be worth taking a look
at ColdSpring and AOP.

Cheers,

Dave

2009/9/1 Gareth Cole <garet...@esus.ie>:

Tom Chiverton

unread,
Sep 2, 2009, 6:27:29 AM9/2/09
to reacto...@googlegroups.com
On Tuesday 01 Sep 2009, Gareth Cole wrote:
> Hi Folks,
>
>
>
> I've a couple of questions about the best way to handle certain scenarios:
>
>
>
> 1. I've got a Rota record. To this record, I've added a custom function

> called updateShifts(). I want this function to be executed every time the
> record is saved, after the save has been done. I thought about over-writing
> the save() method, calling super.save() and then updateShifts().

If you always want to do something before/after save() I'd do that.



> However, I
> also saw an afterSave() method that may be more appropriate to over-write.

afterSave() (etc) let you do things like
<cfset record.attachDelegate(“beforeSave”,addressRecord, “handleSave”) />

and Reactor will call the handleSave method for you.
See http://www.daveshuck.com/blog/2006/02/17/BIG-update-to-Reactor

--

Tom Chiverton
Developer

Tom.Ch...@halliwells.com
3 Hardman Square, Manchester, M3 3EB

****************************************************

This email is sent for and on behalf of Halliwells LLP.

Halliwells LLP is a limited liability partnership registered in England and Wales under registered number OC307980 whose registered office address is at Halliwells LLP, 3 Hardman Square, Spinningfields, Manchester, M3 3EB. A list of members is available for inspection at the registered office together with a list of those non members who are referred to as partners. We use the word “partner” to refer to a member of the LLP, or an employee or consultant with equivalent standing and qualifications. Regulated by the Solicitors Regulation Authority.

CONFIDENTIALITY

This email is intended only for the use of the addressee named above and may be confidential or legally privileged. If you are not the addressee you must not read it and must not use any information contained in nor copy it nor inform any person other than Halliwells LLP or the addressee of its existence or contents. If you have received this email in error please delete it and notify Halliwells LLP IT Department on 0870 365 2500.

For more information about Halliwells LLP visit
www.Halliwells.com.

Gareth Cole

unread,
Sep 2, 2009, 9:34:07 AM9/2/09
to reacto...@googlegroups.com

Thanks for the tips.

 

I think I'll go with my custom save() method for question 1.

 

I had a look at the link, but the attachDelegate() method has been removed from reactor. A later email from Doug mentions that the event model was re-done when iterators were added. I'm not sure the difference of over-writing beforeSave() and afterSave() with just over-writing save()?

 

For question 2, I'd prefer to put the solution in the record. I can't guarantee that the User record wouldn't be saved directly instead of through a service layer every time.

 

I'm guessing I could build a custom load() method that stores the value of 'name' after a load occurs. A custom save() method could then check if this has changed, and call other code if necessary. I'll see how this works later this week.

 

Thanks,

 

Gareth

Tom Chiverton

unread,
Sep 2, 2009, 10:18:13 AM9/2/09
to reacto...@googlegroups.com
On Wednesday 02 Sep 2009, Gareth Cole wrote:
> For question 2, I'd prefer to put the solution in the record. I can't
> guarantee that the User record wouldn't be saved directly instead of
> through a service layer every time.

How can you guarantee it'll always be done via. Reactor then ?


--

Tom Chiverton
Developer

Tom.Ch...@halliwells.com
3 Hardman Square, Manchester, M3 3EB

****************************************************

This email is sent for and on behalf of Halliwells LLP.

Halliwells LLP is a limited liability partnership registered in England and Wales under registered number OC307980 whose registered office address is at Halliwells LLP, 3 Hardman Square, Spinningfields, Manchester, M3 3EB. A list of members is available for inspection at the registered office together with a list of those non members who are referred to as partners. We use the word ?partner? to refer to a member of the LLP, or an employee or consultant with equivalent standing and qualifications. Regulated by the Solicitors Regulation Authority.



CONFIDENTIALITY

This email is intended only for the use of the addressee named above and may be confidential or legally privileged. If you are not the addressee you must not read it and must not use any information contained in nor copy it nor inform any person other than Halliwells LLP or the addressee of its existence or contents. If you have received this email in error please delete it and notify Halliwells LLP IT Department on 0870 365 2500.

For more information about Halliwells LLP visit

Gareth Cole

unread,
Sep 2, 2009, 10:35:58 AM9/2/09
to reacto...@googlegroups.com

For this project, all database interaction is done via reactor.

 

In addition to calling User.save() directly, a User record could be saved indirectly e.g. AnotherRecord.getUserIterator().save().

 

By having this logic directly in the custom User record, I feel more confident that it will always be executed when necessary.

 

I'm not sure if this is the best way to do things, but I use model-glue and it's scaffolding to speed up development. Keeping logic like this in custom reactor objects seems to be the easiest way to handle things

 

-----Original Message-----
From: reacto...@googlegroups.com [mailto:reacto...@googlegroups.com] On Behalf Of Tom Chiverton
Sent: 02 September 2009 15:18
To: reacto...@googlegroups.com
Subject: [reactor-users] Re: 2 reactor questions

 

On Wednesday 02 Sep 2009, Gareth Cole wrote:

Tom Chiverton

unread,
Sep 2, 2009, 11:40:59 AM9/2/09
to reacto...@googlegroups.com
On Wednesday 02 Sep 2009, Gareth Cole wrote:
> In addition to calling User.save() directly, a User record could be saved
> indirectly e.g. AnotherRecord.getUserIterator().save().

Ah ha, yes, overriding save() seems to make sense there.

Gareth Cole

unread,
Sep 4, 2009, 11:11:21 AM9/4/09
to reacto...@googlegroups.com

The solution to question 2 turned out to be quite simple after I discovered the _getInitialTo() method in records.

All I needed to do was add a custom executeSave() method:

 

            <cffunction name="executeSave" access="private" hint="Over-write this function, to check for changes to name" output="false" returntype="void">

                        <cfscript>

                                    var changed = (_getTO().name NEQ _getInitialTO().name);

                                    super.executeSave();

                                   

                                    if (changed){

                                                getNewPassport();

                                    }

                        </cfscript>

            </cffunction>    

 

The important thing was to check if the field changed BEFORE calling super.executeSave(), as that method resets the initial TO.

 

I love the power of reactor. I can build a sophisticated, robust project model with minimum of effort!

 

-----Original Message-----
From: reacto...@googlegroups.com [mailto:reacto...@googlegroups.com] On Behalf Of Gareth Cole
Sent: 01 September 2009 18:11
To: reacto...@googlegroups.com
Subject: [reactor-users] 2 reactor questions

 

Hi Folks,

 

I've a couple of questions about the best way to handle certain scenarios:

 

1.      I've got a Rota record. To this record, I've added a custom function called updateShifts(). I want this function to be executed every time the record is saved, after the save has been done. I thought about over-writing the save() method, calling super.save() and then updateShifts(). However, I also saw an afterSave() method that may be more appropriate to over-write. afterSave() already seems to have a lot of code in it, so I wasn't sure if it's safe to over-write. What's the recommended way of doing this?

2.      I'd like to trigger another custom function if one record property is changed and then saved. As an example, my User record has various fields, one of which is 'name'. If the name is changed and saved, a custom function in the record called getNewPassport() should be executed. Again, I suspect over-writing afterSave() may be the answer, but I've no idea how to monitor the field to see if it changed. Any thoughts?

 

Thanks,

 

Gareth



Reply all
Reply to author
Forward
0 new messages