summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Data/ActiveRecord')
-rw-r--r--framework/Data/ActiveRecord/TActiveRecordConfig.php108
-rw-r--r--framework/Data/ActiveRecord/TActiveRecordGateway.php62
-rw-r--r--framework/Data/ActiveRecord/TActiveRecordManager.php19
3 files changed, 174 insertions, 15 deletions
diff --git a/framework/Data/ActiveRecord/TActiveRecordConfig.php b/framework/Data/ActiveRecord/TActiveRecordConfig.php
new file mode 100644
index 00000000..5670e64c
--- /dev/null
+++ b/framework/Data/ActiveRecord/TActiveRecordConfig.php
@@ -0,0 +1,108 @@
+<?php
+/**
+ * TActiveRecordConfig class file.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005-2007 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Data.ActiveRecord
+ */
+
+Prado::using('System.Data.TDataSourceConfig');
+
+/**
+ * TActiveRecordConfig module configuration class.
+ *
+ * Database configuration for the default ActiveRecord manager instance.
+ *
+ * Example: application.xml configuration
+ * <code>
+ * <modules>
+ * <module class="System.Data.ActiveRecord.TActiveRecordConfig" EnableCache="true">
+ * <database ConnectionString="mysql:host=localhost;dbname=test"
+ * Username="dbuser" Password="dbpass" />
+ * </module>
+ * </modules>
+ * </code>
+ *
+ * MySQL database definition:
+ * <code>
+ * CREATE TABLE `blogs` (
+ * `blog_id` int(10) unsigned NOT NULL auto_increment,
+ * `blog_name` varchar(255) NOT NULL,
+ * `blog_author` varchar(255) NOT NULL,
+ * PRIMARY KEY (`blog_id`)
+ * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+ * </code>
+ *
+ * Record php class:
+ * <code>
+ * class Blogs extends TActiveRecord
+ * {
+ * public $blog_id;
+ * public $blog_name;
+ * public $blog_author;
+ *
+ * public static function finder()
+ * {
+ * return self::getRecordFinder('Blogs');
+ * }
+ * }
+ * </code>
+ *
+ * Usage example:
+ * <code>
+ * class Home extends TPage
+ * {
+ * function onLoad($param)
+ * {
+ * $blogs = Blogs::finder()->findAll();
+ * print_r($blogs);
+ * }
+ * }
+ * </code>
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data.ActiveRecord
+ * @since 3.1
+ */
+class TActiveRecordConfig extends TDataSourceConfig
+{
+ private $_enableCache=false;
+
+ /**
+ * Initialize the active record manager.
+ * @param TXmlDocument xml configuration.
+ */
+ public function init($xml)
+ {
+ parent::init($xml);
+ Prado::using('System.Data.ActiveRecord.TActiveRecordManager');
+ $manager = TActiveRecordManager::getInstance();
+ if($this->getEnableCache())
+ $manager->setCache($this->getApplication()->getCache());
+ $manager->setDbConnection($this->getDbConnection());
+ }
+
+ /**
+ * Set true to cache the table meta data.
+ * @param boolean true to cache sqlmap instance.
+ */
+ public function setEnableCache($value)
+ {
+ $this->_enableCache = TPropertyValue::ensureBoolean($value, false);
+ }
+
+ /**
+ * @return boolean true if table meta data should be cached, false otherwise.
+ */
+ public function getEnableCache()
+ {
+ return $this->_enableCache;
+ }
+}
+
+?> \ No newline at end of file
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;
}
/**
diff --git a/framework/Data/ActiveRecord/TActiveRecordManager.php b/framework/Data/ActiveRecord/TActiveRecordManager.php
index 883abbf2..7f239a34 100644
--- a/framework/Data/ActiveRecord/TActiveRecordManager.php
+++ b/framework/Data/ActiveRecord/TActiveRecordManager.php
@@ -11,6 +11,7 @@
*/
Prado::using('System.Data.TDbConnection');
+Prado::using('System.Data.ActiveRecord.TActiveRecord');
Prado::using('System.Data.ActiveRecord.Exceptions.TActiveRecordException');
Prado::using('System.Data.ActiveRecord.TActiveRecordGateway');
Prado::using('System.Data.ActiveRecord.TActiveRecordStateRegistry');
@@ -46,6 +47,24 @@ class TActiveRecordManager extends TComponent
private $_meta=array();
private $_connection;
+ private $_cache;
+
+ /**
+ * @return ICache application cache.
+ */
+ public function getCache()
+ {
+ return $this->_cache;
+ }
+
+ /**
+ * @param ICache application cache
+ */
+ public function setCache($value)
+ {
+ $this->_cache=$value;
+ }
+
/**
* @param TDbConnection default database connection
*/