title: Best way to handle dirty state in an ORM model
body: <p>I don't want anyone saying <em>"you should not reinvent the wheel, use an open source ORM"</em>; I have an immediate requirement and cannot switch.</p>
  
  <p>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.</p>
  
  <p>Here is my approach:</p>
  
  <ul>
  <li>I want to avoid runtime introspection (i.e. guessing attributes).</li>
  <li>I don't want to use a CLI code generator to generate getters and setters (really I use the NetBeans one, using ALT+INSERT).</li>
  <li>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.</li>
  </ul>
  
  <p>I have an Abstract class called <code>AbstractModel</code> that all the models inherit. It has a public method called <code>isDirty()</code> 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.</p>
  
  <p><strong>The issue is:</strong> is there a way to raise the internal flag <code>"is_dirty"</code> without coding in each setter <code>$this-&gt;is_dirty = true</code>? I mean: I want to have the setters as <code>$this-&gt;attr = $value</code> most of the time, except a code change is needed for business logic.</p>
  
  <p>Other limitation is that I cannot rely on <code>__set</code> because on the concrete model class the attributes already exists as private, so <code>__set</code> is never called on the setters.</p>
  
  <p>Any ideas? Code examples from others ORMs are accepted.</p>
  
  <p>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.</p>
  
  <p>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 <code>__set</code> and have some code there to deal with the <code>"is_dirty"</code> flag, but this breaks the POPO concept a little, and it's ugly.</p>