mage_core_model_resource---------系统资源模型分析!!!!

本文解析了Magento中的核心资源模型类Mage_Core_Model_Resource_Abstract,介绍了如何建立数据库连接、获取表名及配置信息,并概述了其在事务处理、日期格式化等方面的功能。
mage_core_model_resource_abstract.php 1.1 //* Instances of classes for connection types protected $_connectionTypes = array(); 1.2 //Instances of actual connections protected $_connections = array(); 1.3 // Creates a connection to resource whenever needed @return mixed public function getConnection($name) 1.4 //被getConnection调用 protected function _getDefaultConnection($requiredConnectionName) 1.5 //Get connection type instance @return Mage_Core_Model_Resource_Type_Abstract public function getConnectionTypeInstance($type) 1.6 //Get resource entity @return Varien_Simplexml_Config public function getEntity($model, $entity) 1.7 //Get resource table name @return string public function getTableName($modelEntity) 1.8 public function createConnection($name, $type, $config) 基本上是表信息的取得,在varien_db_adapter_pro_mysql的基础上取到connection。 **************************************************************************** 由此类引申出来如可得到配置文件里面的table值--> $resourceModel = (string) Mage::getConfig()->getNode()->global->models-> {$model}->resourceModel; $entityConfig = $this->getEntity($resourceModel, $entity); if ($entityConfig) { $tableName = (string)$entityConfig->table; 附上一段配置文件代码: <global><models><catalog><class>Mage_Catalog_Model</class><resourcemodel>catalog_resource_eav_mysql4</resourcemodel></catalog><catalog_resource_eav_mysql4><class>Mage_Catalog_Model_Resource_Eav_Mysql4</class><entities><product><table>catalog_product_entity</table></product> 2 abstract class Mage_Core_Model_Resource_Abstract 2.1 // * Start resource transaction @return Mage_Core_Model_Resource_Abstract public function beginTransaction() { $this-&gt;_getWriteAdapter()-&gt;beginTransaction(); return $this; } 2.2 //Subscribe some callback to transaction commit @return Mage_Core_Model_Resource_Abstract public function addCommitCallback($callback) 2.3 //Commit resource transaction @return Mage_Core_Model_Resource_Abstract public function commit() 2.4 //Roll back resource transaction @return Mage_Core_Model_Resource_Abstract public function rollBack() 2.5 // Format date to internal format public function formatDate($date, $includeTime=true) 2.6 public function mktime($str) 2.7 //Serialize specified field in an object protected function _serializeField(Varien_Object $object, $field, $defaultValue = null, $unsetEmpty = false) 2.8 //Unserialize Varien_Object field in an object protected function _unserializeField(Varien_Object $object, $field, $defaultValue = null) 数据库 2.1 事务方面的处理, 2.2 序列化字段 2.3 时间等格式处理。 比较明显的就是事务的处理!! 3 由 abstract class Mage_Core_Model_Resource_Abstract 可以延伸至 abstract class Mage_Core_Model_Resource_Type_Abstract protected $_entityClass = 'Mage_Core_Model_Resource_Entity_Abstract'; public function getEntityClass() { return $this-&gt;_entityClass; } public function setName($name) { $this-&gt;_name = $name; } public function getName() { return $this-&gt;_name; } 4 延伸至 abstract class Mage_Core_Model_Resource_Entity_Abstract { protected $_name = null; protected $_config = array(); public function __construct($config) { $this-&gt;_config = $config; } public function getConfig($key='') { if (''===$key) { return $this-&gt;_config; } elseif (isset($this-&gt;_config-&gt;$key)) { return $this-&gt;_config-&gt;$key; } else { return false; } } } 5 class Mage_Core_Model_Resource_Type_Db_Pdo_Mysql extends Mage_Core_Model_Resource_Type_Db 5.1 //* @return Varien_Db_Adapter_Pdo_Mysql public function getConnection($config) 5.2 //* Create and return DB adapter object instance @param array $configArr Connection config @return Varien_Db_Adapter_Pdo_Mysql protected function _getDbAdapterInstance($configArr) 5.3 * Retrieve DB adapter class name * @return string */ protected function _getDbAdapterClassName() //得到----&gt; Varien_Db_Adapter_Pdo_Mysql,做一个桥梁!!! 总体来说resource这块的功能,作为一个连接varien_db_adapter_pro_mysql的桥梁,得 到连接 又通过xml得到配置信息的表,主键等信息,为数据库操作作准备。同时有数据库事务方 面的处理。数据格式的处理 </entities></catalog_resource_eav_mysql4></models></global>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值