CHAPTER 13 ■ DATABASE PATTERNS
295
persistent storage. Magic is nice, but clarity is nicer. It may be better to require client code to pass some
kind of flag into the constructor in order to queue the new object for insertion.
I also need to add some code to the Mapper class:
// Mapper
function createObject( $array ) {
$old = $this->getFromMap( $array['id']);
if ( $old ) { return $old; }
$obj = $this->doCreateObject( $array );
$this->addToMap( $obj );
$obj->markClean();
return $obj;
}
Because setting up an object involves marking it new via the constructor’s call to
ObjectWatcher::addNew(), I must call markClean(), or every single object extracted from the database will
be saved at the end of the request, which is not what I want. ...