I tried the variant with overriding the behaviors() method and it works fine. The question is, how can I correctly attach a behavior at runtime and make it handle the afterFind method.
The idea behind is the following: I have numerical timestamps in my database, so they are just ints. When working with the model doing calculations and stuff, I want them to stay numeric.
Only when I display the values in a form, I would like them to be converted into string dates and also accept string dates as input. Thus, I will put afterFind and beforeSave into the behavior.
But when I add it to the model ‘statically’ by overriding behaviors(), it will convert the timestamps into strings all the time. For displaying, I would like to change the behavior of the model, but the default should be as it is now.
I found out that I can attach the behavior to the model at runtime and it takes it. But when I do a findAll and get a bunch of AR objects, they dont have it. This only works if I change the models design.
I might need to do another step to kind of ‘publish’ the changed model behavior to the generated AR objects, but I am not getting it. And this is what I am looking for
How can I make sure that the AR objects take over the behavior that I attached to the model at runtime?
PS: This also relates to my other topic at http://www.yiiframework.com/forum/index.php?/topic/4346-extend-model-with-new-attributes-and-populate-by-join-query/
I am thinking of a behavior, that will make extra attributes, that have been read during a join query, available with the model as if they had been in the model definition. As an alternative for relations, which are nice for forms, but not that helpful for grids, where you might want sorting, filtering or paging. Being able to extend your model at runtime would be a nice opportunity.