From 2570226fbac3e26b1e94896b50d1db4bc1aa3308 Mon Sep 17 00:00:00 2001 From: wei <> Date: Sun, 17 Dec 2006 22:20:50 +0000 Subject: Add TDataSourceConfig, TSqlMapConfig, TActiveRecordConfig --- .../Data/ActiveRecord/TActiveRecordGateway.php | 62 ++++++++++++++++------ 1 file changed, 47 insertions(+), 15 deletions(-) (limited to 'framework/Data/ActiveRecord/TActiveRecordGateway.php') diff --git a/framework/Data/ActiveRecord/TActiveRecordGateway.php b/framework/Data/ActiveRecord/TActiveRecordGateway.php index 19c8552f..e7ea5e46 100644 --- a/framework/Data/ActiveRecord/TActiveRecordGateway.php +++ b/framework/Data/ActiveRecord/TActiveRecordGateway.php @@ -22,7 +22,7 @@ class TActiveRecordGateway extends TComponent { private $_manager; - private $_tables=array(); + private $_tables=array(); //meta data cache. /** * Property name for optional table name in TActiveRecord. @@ -68,35 +68,67 @@ class TActiveRecordGateway extends TComponent } /** - * Gets the meta data for given database and table. + * @param TActiveRecord active record. + * @return TDbMetaData table meta data, null if not found. */ - public function getMetaData(TActiveRecord $record) + protected function getCachedMetaData($record) { $type=get_class($record); - if(!isset($this->_tables[$type])) + if(isset($this->_tables[$type])) + return $this->_tables[$type]; + if(($cache=$this->getManager()->getCache())!==null) { - $conn = $record->getDbConnection(); - $inspector = $this->getManager()->getTableInspector($conn); - $table = $this->getTableName($record); - $this->_tables[$type] = $inspector->getTableMetaData($table); + //force loading of the table inspector to load the required classes + // before unserializing cached meta data. + $this->getManager()->getTableInspector($record->getDbConnection()); + $data = $cache->get($this->getMetaDataCacheKey($record)); + if($data !== false && $data !== null) + { + $this->_tables[$type] = $data; + return $data; + } } - return $this->_tables[$type]; } /** - * @param array table meta data. + * @param TActiveRecord active record. + * @return string cache key, using connection string + record class name + */ + protected function getMetaDataCacheKey($record) + { + $conn = $record->getDbConnection()->getConnectionString(); + return $conn.':'.get_class($record); + } + + /** + * Cache the meta data, tries the application cache if applicable. + * @param TActiveRecord active record. + * @param TDbMetaData table meta data + * @return TDbMetaData table meta data. */ - public function setAllMetaData($data) + protected function cacheMetaData($record,$data) { - $this->_tables=$data; + $type = get_class($record); + if(($cache=$this->getManager()->getCache())!==null) + $cache->set($this->getMetaDataCacheKey($record), $data); + $this->_tables[$type] = $data; + return $data; } /** - * @return array all table meta data. + * Gets the meta data for given database and table. */ - public function getAllMetaData() + public function getMetaData(TActiveRecord $record) { - return $this->_tables; + $type=get_class($record); + if(!($data = $this->getCachedMetaData($record))) + { + $conn = $record->getDbConnection(); + $inspector = $this->getManager()->getTableInspector($conn); + $table = $this->getTableName($record); + $data = $this->cacheMetaData($record,$inspector->getTableMetaData($table)); + } + return $data; } /** -- cgit v1.2.3