I don't want anyone saying "you should not reinvent the wheel, use an open source ORM"; I have an immediate requirement and cannot switch.
I'm doing a little ORM, that supports caching. Even not supporting caching, I would need this feature anyways, to know when to write an object to storage or not. The pattern is DataMapper.
Here is my approach:
- I want to avoid runtime introspection (i.e. guessing attributes).
- I don't want to use a CLI code generator to generate getters and setters (really I use the NetBeans one, using ALT+INSERT).
- I want the model to be the closest to a POPO (plain old PHP object). I mean: private attributes, "hardcoded" getters and setters for each attribute.
I have an Abstract class called AbstractModel
that all the models inherit. It has a public method called isDirty()
with a private (can be protected too, if needed) attribute called is_dirty. It must return true or false depending if there is a change on the object data or not since it was loaded.
The issue is: is there a way to raise the internal flag "is_dirty"
without coding in each setter $this->is_dirty = true
? I mean: I want to have the setters as $this->attr = $value
most of the time, except a code change is needed for business logic.
Other limitation is that I cannot rely on __set
because on the concrete model class the attributes already exists as private, so __set
is never called on the setters.
Any ideas? Code examples from others ORMs are accepted.
One of my ideas was to modify the NetBeans setters template, but I think there should be a way of doing this without relying on the IDE.
Another thought I had was creating the setters and then change the private attribute's name with an underscore or something. This way the setter would call to __set
and have some code there to deal with the "is_dirty"
flag, but this breaks the POPO concept a little, and it's ugly.
I would make a proxy to set for example:
Then each child class would implemnt its setter by calling
_set()
and never set the property directly. Further, you can always inject more class specific code into each sub class's_set
and just callparent::set($attr, $processedValue)
if needed. Then if you want to use magic methods you make those proxy to property method that proxies to_set
. I suppose this isnt very POPO though.though this post is old BUT how about using events to notify listeners when isDirty() happens? I would approach the solution with events.
I'm kinda confused how what-you-call-Model is related to ORM. It's kinda confusing. Especially since in MVC the Model is a layer (at least, thats how i understand it, and your "Models" seem to me more like Domain Objects).
I will assume that what you have is a code that looks like this:
And, i will assume that what-you-call-Model has two methods, designer to be used by data mappers:
getParameters()
andsetParameters()
. And that you callisDirty()
before mapper stores what-you-call-Model's state and callcleanState()
- when mapper pull data into what-you-call-Model.This would make the data mapper methods look like:
Up to this part you should already have everything similar...
Now here is what the
isDirty()
andcleanState()
methods should do: