The logic to build the SQL and collect the bind data doesn't reside inside the ActiveRecord class.
For example, this is the static find method that resides inside the ActiveRecord class:
PHP Code:public static function _find($pClassName,$pOptions) {
if(self::isConnected()===false) throw new Exception('A database Adaptor has not been set for the '.__CLASS__.' Class.');
$model = ActiveRecordModelConfig::getModelConfig($pClassName);
$mode = !empty($pOptions) && is_array($pOptions[0])===false?array_shift($pOptions):self::findAll;
ActiveRecordSelectNode::resetCount();
$node = new ActiveRecordSelectNode($model,new ActiveRecordFindConfig(!empty($pOptions)?$pOptions[0]:array()));
$select = strcasecmp($mode,self::findCount)==0?new ActiveRecordCount($node,$pOptions):new ActiveRecordSelect($node,$pOptions);
//echo '<p>',$select->toSql(),'</p>';
$stmt = $select->query(self::$_db);
$collectionAgent = new ActiveRecordCollectionAgent($select);
if(strcmp($mode,self::findCount)==0) { return $stmt->fetchColumn(); }
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$collectionAgent->process($row,$node);
}
$records = $collectionAgent->getRecords();
return strcmp($mode,self::findOne)==0?!empty($records)?$records[0]:null:$records;
}
To be honest I just like and find the aesthetics/interface nicer to use. More of a personal preference then anything. Although this implementation in many ways is a border between the two because all the data and sql building logic are kept in separate objects that the ActiveRecord class manages.Originally Posted by allspiritseve
This the code for the save method. As you can see the responsibility of generating the save sql and executing it is not responsibility of the ActiveRecord directly.
Personally, I rather do this:PHP Code:public function save($pValidate=true) {
$save = new ActiveRecordSave($this);
return $save->query(self::$_db);
}
Then this:PHP Code:$entry = new Entry(1);
Managing one vs. managing two… I'll take one.PHP Code:$entryGateway = new EntryGateway($db);
$entry = $entrygateway->find('one',array('id'=>1));
I've never seen the benefit of using the gateway if the active record has the sql creation logic and property repository factored out of it directly.
So unlike other implementation you may have come across the one I am working on here does actually separate the code. However, to me that's just good programming. Making sure that each class only has two or three main responsibilities and not cramming everything into one just because you can.
So I guess you could say this approach is more or less a hybrid between the Gateway and ActiveRecord perhaps…







Bookmarks