Magento通过可配置(configurable)商品Id获得子类Ids

通过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是一个二维数组。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值