
Originally Posted by
arborint
With all this theoretical talk I thought I would hack something up just to learn a little about the subject. Here is some PHP5 code sets up handlers for get/set and tracks whether a property has been fetched or is dirty. I assume that an ORM would plug into the set handler to fetch the data and then do the commit. Obviously a system to map the properties for each object to a table/field/key needs to be added. There is example code a the bottom so you can run it.
I think that sort of thing is an option to look at. It gives the user less control of their design but has better performance.
In this type of situation construction becomes difficult because you cant use setters. So any persistent object must: have getters/setters (__call()), have a full constructor for every way an object can be constructed, extend PersistenceBase and not have any private properties. There are other subtle things that need to be kept in mind with this approach.
I want to articulate what my thoughts are now in regards to lazy loading a code generated query.
Heres our xml file:
PHP Code:
<persist>
<class name="Team" table="TEAMS">
<field name="name" fieldtype="string" column="NAME" />
<field name="profit" fieldtype="Money" column="TOTAL_PROFITS" columnType="float" />
</class>
</persist>
The query:
PHP Code:
$oql = 'SELECT UNIQUE Team WHERE Team.profit > ?';
$query = $pm->newQuery($oql);
$team = $query->execute(10000);
The Query Class:
PHP Code:
class Query {
function execute() {
if(!$this->shouldOQLComplile()) {
$this->compileOQL();
}
$args = func_get_args();
return include $this->oqlAsFile();
}
function shouldOQLCompile() {
// If xmlMap has been changed since
// last time code was generated
// Then generate code again.
// If no code has been generated
// Then generate code
return (!file_exists($this->oqlAsFile())
|| filemtime($this->oqlAsFile()) <
filemtime($this->xmlMapFileName));
}
function oqlAsFile() {
return; //this->oql as a proper unique filename;
}
function compileOQL() {
// parse $this->oql;
// create file with proper
// generated code;
}
}
And the generated code (file returned by oqlAsFile):
PHP Code:
$sql = 'SELECT * FROM TEAMS' .
' WHERE TEAMS.TOTAL_PROFITS > ?';
$stmt = $this->conn->prepare($sql);
$stmt->setFloat($args[0]);
$recordSet = $stmt->executeQuery();
$recordSet->next();
$result = new Team();
$result->setName($recordSet->getString('NAME'));
$result->setProfits($recordSet->getMoney('TOTAL_PROFITS'));
$this->persistenceManager->registerClean($result);
return $result;
So for each query in the system a file is code generated on the first run. You get good performance, and dont have to deal with classMaps except on the first run.
Might be able to generate a class or something instead of returning the value of the included file but I think this explains it.
Bookmarks