00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Core_Model_Resource
00033 {
00034
00035 const AUTO_UPDATE_CACHE_KEY = 'DB_AUTOUPDATE';
00036 const AUTO_UPDATE_ONCE = 0;
00037 const AUTO_UPDATE_NEVER = -1;
00038 const AUTO_UPDATE_ALWAYS = 1;
00039
00040
00041
00042
00043
00044
00045 protected $_connectionTypes = array();
00046
00047
00048
00049
00050
00051
00052 protected $_connections = array();
00053
00054
00055
00056
00057
00058
00059 protected $_entities = array();
00060
00061 protected $_mappedTableNames;
00062
00063
00064
00065
00066
00067
00068
00069 public function getConnection($name)
00070 {
00071 if (isset($this->_connections[$name])) {
00072 return $this->_connections[$name];
00073 }
00074 $connConfig = Mage::getConfig()->getResourceConnectionConfig($name);
00075
00076 if (!$connConfig || !$connConfig->is('active', 1)) {
00077 return false;
00078 }
00079 $origName = $connConfig->getParent()->getName();
00080
00081 if (isset($this->_connections[$origName])) {
00082 $this->_connections[$name] = $this->_connections[$origName];
00083 return $this->_connections[$origName];
00084 }
00085
00086 $typeInstance = $this->getConnectionTypeInstance((string)$connConfig->type);
00087 $conn = $typeInstance->getConnection($connConfig);
00088
00089 $this->_connections[$name] = $conn;
00090 if ($origName!==$name) {
00091 $this->_connections[$origName] = $conn;
00092 }
00093 return $conn;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 public function getConnectionTypeInstance($type)
00105 {
00106 if (!isset($this->_connectionTypes[$type])) {
00107 $config = Mage::getConfig()->getResourceTypeConfig($type);
00108 $typeClass = $config->getClassName();
00109 $this->_connectionTypes[$type] = new $typeClass();
00110 }
00111 return $this->_connectionTypes[$type];
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121 public function getEntity($model, $entity)
00122 {
00123
00124 return Mage::getConfig()->getNode()->global->models->{$model}->entities->{$entity};
00125 }
00126
00127
00128
00129
00130
00131
00132
00133 public function getTableName($modelEntity)
00134 {
00135 $arr = explode('/', $modelEntity);
00136 if (isset($arr[1])) {
00137 list($model, $entity) = $arr;
00138
00139 $resourceModel = (string) Mage::getConfig()->getNode()->global->models->{$model}->resourceModel;
00140 $entityConfig = $this->getEntity($resourceModel, $entity);
00141 if ($entityConfig) {
00142 $tableName = (string)$entityConfig->table;
00143 } else {
00144 Mage::throwException(Mage::helper('core')->__('Can\'t retrieve entity config: %s', $modelEntity));
00145 }
00146 } else {
00147 $tableName = $modelEntity;
00148 }
00149
00150 Mage::dispatchEvent('resource_get_tablename', array('resource' => $this, 'model_entity' => $modelEntity, 'table_name' => $tableName));
00151 $mappedTableName = $this->getMappedTableName($tableName);
00152 if ($mappedTableName) {
00153 $tableName = $mappedTableName;
00154 } else {
00155 $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
00156 $tableName = $tablePrefix . $tableName;
00157 }
00158
00159 return $tableName;
00160 }
00161
00162 public function setMappedTableName($tableName, $mappedName)
00163 {
00164 $this->_mappedTableNames[$tableName] = $mappedName;
00165 return $this;
00166 }
00167
00168 public function getMappedTableName($tableName)
00169 {
00170 if (isset($this->_mappedTableNames[$tableName])) {
00171 return $this->_mappedTableNames[$tableName];
00172 } else {
00173 return false;
00174 }
00175 }
00176
00177 public function cleanDbRow(&$row)
00178 {
00179 if (!empty($row) && is_array($row)) {
00180 foreach ($row as $key=>&$value) {
00181 if (is_string($value) && $value==='0000-00-00 00:00:00') {
00182 $value = '';
00183 }
00184 }
00185 }
00186 return $this;
00187 }
00188
00189 public function createConnection($name, $type, $config)
00190 {
00191 if (!isset($this->_connections[$name])) {
00192 $typeObj = $this->getConnectionTypeInstance($type);
00193 $this->_connections[$name] = $typeObj->getConnection($config);
00194 }
00195 return $this->_connections[$name];
00196 }
00197
00198 public function checkDbConnection()
00199 {
00200 if (!$this->getConnection('core_read')) {
00201
00202 }
00203 }
00204
00205 public function getAutoUpdate()
00206 {
00207 return self::AUTO_UPDATE_ALWAYS;
00208 #return Mage::app()->loadCache(self::AUTO_UPDATE_CACHE_KEY);
00209 }
00210
00211 public function setAutoUpdate($value)
00212 {
00213 #Mage::app()->saveCache($value, self::AUTO_UPDATE_CACHE_KEY);
00214 return $this;
00215 }
00216
00217 }