通过productId获取子类ID:
$childProductIds = Mage::getModel('catalog/product_type_configurable') ->getChildrenIds($product_id);
getChildrenIds():
Mage_Catalog_Model_Product_Type_Configurable.php
/** * Retrieve Required children ids * Return grouped array, ex array( * group => array(ids) * ) * * @param int $parentId * @param bool $required * @return array */ public function getChildrenIds($parentId, $required = true) { return Mage::getResourceSingleton('catalog/product_type_configurable') ->getChildrenIds($parentId, $required); }
getChildrenIds():
Mage_Catalog_Model_Resource_Product_type_Configurable.php
/** * Retrieve Required children ids * Return grouped array, ex array( * group => array(ids) * ) * * @param int $parentId * @param bool $required * @return array */ public function getChildrenIds($parentId, $required = true) { $childrenIds = array(); $select = $this->_getReadAdapter()->select() ->from(array('l' => $this->getMainTable()), array('product_id', 'parent_id')) ->join( array('e' => $this->getTable('catalog/product')), 'e.entity_id = l.product_id AND e.required_options = 0', array() ) ->where('parent_id = ?', $parentId); $childrenIds = array(0 => array()); foreach ($this->_getReadAdapter()->fetchAll($select) as $row) { $childrenIds[0][$row['product_id']] = $row['product_id']; } return $childrenIds; }
_getReadAdapter():
Mage_Core_Model_Resource_Db_Abstract.php
这里涉及到了数据库的事务,如果事务开始使用写连接,否则使用读连接:
/** * Retrieve connection for read data * * @return Varien_Db_Adapter_Interface */ protected function _getReadAdapter() { $writeAdapter = $this->_getWriteAdapter(); if ($writeAdapter && $writeAdapter->getTransactionLevel() > 0) { // if transaction is started we should use write connection for reading return $writeAdapter; } return $this->_getConnection('read'); }
/** * Retrieve connection for write data * * @return Varien_Db_Adapter_Interface */ protected function _getWriteAdapter() { return $this->_getConnection('write'); }
_getConnection()
根据参数类型获得连接类型:
/** * Get connection by name or type * * @param string $connectionName * @return Zend_Db_Adapter_Abstract */ protected function _getConnection($connectionName) { if (isset($this->_connections[$connectionName])) { return $this->_connections[$connectionName]; } if (!empty($this->_resourcePrefix)) { $this->_connections[$connectionName] = $this->_resources->getConnection( $this->_resourcePrefix . '_' . $connectionName); } else { $this->_connections[$connectionName] = $this->_resources->getConnection($connectionName); } return $this->_connections[$connectionName]; }
_connections():
/** * Connections cache for this resource model * * @var array */ protected $_connections = array();
getMainTable():
返回主表名,从module/table中获得
/** * Returns main table name - extracted from "module/table" style and * validated by db adapter * * @return string */ public function getMainTable() { if (empty($this->_mainTable)) { Mage::throwException(Mage::helper('core')->__('Empty main table name')); } return $this->getTable($this->_mainTable); }
getTable():
为实体得到表名,通过db适配器进行验证
/** * Get table name for the entity, validated by db adapter * * @param string $entityName * @return string */ public function getTable($entityName) { if (is_array($entityName)) { $cacheName = join('@', $entityName); list($entityName, $entitySuffix) = $entityName; } else { $cacheName = $entityName; $entitySuffix = null; } if (isset($this->_tables[$cacheName])) { return $this->_tables[$cacheName]; } if (strpos($entityName, '/')) { if (!is_null($entitySuffix)) { $modelEntity = array($entityName, $entitySuffix); } else { $modelEntity = $entityName; } $this->_tables[$cacheName] = $this->_resources->getTableName($modelEntity); } else if (!empty($this->_resourceModel)) { $entityName = sprintf('%s/%s', $this->_resourceModel, $entityName); if (!is_null($entitySuffix)) { $modelEntity = array($entityName, $entitySuffix); } else { $modelEntity = $entityName; } $this->_tables[$cacheName] = $this->_resources->getTableName($modelEntity); } else { if (!is_null($entitySuffix)) { $entityName .= '_' . $entitySuffix; } $this->_tables[$cacheName] = $entityName; } return $this->_tables[$cacheName]; }
以上就是一个几乎完整的获得子类ID方法下包含的所有方法,通过这些方法可以看出来所得到的子类ID是一个二维数组。