Ticket #1208 (closed defect: fixed)

Opened 21 months ago

Last modified 18 months ago

ability to define record level handlers for custom fields

Reported by: ccunningham Owned by: romanb
Priority: minor Milestone: 1.1.0-ALPHA1
Component: Listeners Version: 1.0.0
Severity: Keywords:
Cc: Has Test: no
Status: Pending Core Response Has Patch: no

Description

It would be nice if you could put the equivalent of a preHydrate handler in your record class to add custom/calculated fields to your model. The current method requires a HydrateListener? which works, but requires you to either have one for each type of custom field you want, or just one that has to do some introspection and switches to handle different things. If you have a dozen models each of which may need their own set of custom fields, this becomes a real pain. If you could just throw it in the record class like:

public function preHydrate( $event ) {

if ( $this->due_date < ... ) {

$this->status = 'current';

} else {

$this->status = 'expired';

}

}

Change History

Changed 20 months ago by guilhermeblanco

  • status changed from new to closed
  • resolution set to invalid

You can do it through postHydrate listener and access the element through $event->data.

Marking the ticket as invalid. If it's not the case, reopen the ticket with a test case of expected behavior.

Changed 20 months ago by romanb

  • milestone set to 1.0.0-BETA1

Changed 19 months ago by ccunningham

  • status changed from closed to reopened
  • resolution invalid deleted

Yes, it can be done, however it's a real pain.

I have a number of classes I want to do this on. I have a few entity classes representing people, I have some other classes which need a piece of data for 'name' where it's based on whether a custom name has been chosen, etc. It can be done through the listener interface, however that would involve having one listener class with a big switch statement ( to perform the desired custom field for each model type ) or create a bunch of different listener classes and assign them on a per table basis. Essentially I either have one big ugly global listener that isn't needed on all objects, or a bunch of small simple ones I have to attach on each model type.

While that does do what I want, it isn't convenient or simple...

Why can't I just put a postHydrate() function in each model class and do what I want there? It would greatly simplify the process and let you keep these little snippets nicely nested in their own models, so instead of having all these listener classes laying around I have a nice clean model.

Changed 19 months ago by jwage

  • version changed from 0.11 to 1.0
  • milestone changed from Unknown to 1.1.0

Changed 18 months ago by jwage

  • status changed from reopened to closed
  • resolution set to fixed

In r5025 this was fixed.

Note: See TracTickets for help on using tickets.