Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Get old values from fActiveRecord

posted by bense 7 years ago

Hi everybody

Before storing a record I need to know some old values (those which are in the database before saving). How can I achieve this? If I try to load the "old" record with 'new XY ($id)' I get the same one, because it is, let's say, a singleton.

Of course this is a simple problem, but I can't find the solution at the documentation (maybe I'm looking at the wrong position)...

Thanks for your help!

Ben

Hi again

Meanwhile, I found a solution for the problem. I wrote a function in my record-class (extending fActiveRecord) which calls retrieveOld().

But if someone knows a "direct" solution without writing code...suggestions are still welcome. :-)

Ben

posted by bense 7 years ago

bense, this should be done in an ORM hook. Old values are passed as an argument to those callbacks see:

http://flourishlib.com/api/fORM#registerHookCallback

posted by mattsah 7 years ago

Hi msahagian

I have something like a service oriented architecture. So storing an object is a service-method. In this case, I want to add a special processing before storing the fActiveRecord. If I need to register a callback-function...in my opinion the functionality is not really encapsulated. I may be wrong, but it seems that it would be a cleaner solution if I can implement the needed functionality in the service-method itself.

Ben

posted by bense 7 years ago

Quite the contrary it is BETTER encapsulated. Even if storing an object is a service method, the object itself should be self-contained. All of it's validation and business logic should occur internally in the Model. The fact that it's a callback, if registered properly, means that this logic will occur regardless of where it's stored from.

I cannot think of a hypothetical situation where I would need to know the old values of a record for some sort of manipulation outside of the Models. If you can make your particular case a bit clearer then I can give you a more detailed example. What are the values on the Record? What are the old values you need? What are you doing with the old values?

posted by mattsah 7 years ago

Hmm, ok, let's say I have a service called 'AddressChanger'. In this service, i change the object 'Address'. Basically I get the changed address-object and I will call store(). In addition, I want to send a message to the old address 'Goodbye' and a message to the new address 'Hello!'. For this I need the old address-values. I don't want the model to execute this message-functionality. <-- Sorry, it's a little bit a strange example. :-)

You may say that i should only give the changed values to the service (instead of the already changed object), but I have a converter, which is able to build objects directly from user input (by a form, json, or whatever). I always work with "complete" objects and not single values.

Hope my explanations are clear to you... :-)

posted by bense 7 years ago

I'm not sure what you mean by sending a message -- I'm assuming you mean something like e-mail addresseses. If this is the case, I would agree that the sending of the message should not be in the model, it should be in the controller. In this case though your controller is responsible for storing the old record. My initial suggestion is simply to do something like the following:

$old_record = $record->replicate();

try {
    $record->populate();
    $record->store();
    //
    // send goodbye message using $old_record and hello message using $record
    //
} catch (fValidaitonException $e) {
    //
    // handle issues with new data
    //
}
posted by mattsah 7 years ago

I see...thanks for your help, msahagian!

posted by bense 7 years ago