Magento 获取商品所在的所有分类

$product = Mage::getModel('catalog/product')->load($product_id);

$category_ids = $product->getCategoryIds();

$product->getCategoryIds();

  • 先判断数据里面是否有category_ids这个属性;
  • 如果不存在的话在看属性的锁机制,如果属性被锁则打开锁
  • 然后通过资源模型查询数据表
  • 如果锁打开了,最后在关上锁
/**
     * Retrieve assigned category Ids
     *
     * @return array
     */
public function getCategoryIds()
{
    if (! $this->hasData('category_ids')) {
        $wasLocked = false;
        if ($this->isLockedAttribute('category_ids')) {
            $wasLocked = true;
            $this->unlockAttribute('category_ids');
        }
        $ids = $this->_getResource()->getCategoryIds($this);
        $this->setData('category_ids', $ids);
        if ($wasLocked) {
            $this->lockAttribute('category_ids');
        }
    }

    return (array) $this->_getData('category_ids');
}

$this->isLockedAttribute();

/**
     * Retrieve locked attributes
     *
     * @return boolean
     */
public function isLockedAttribute($attributeCode)
{
    return isset($this->_lockedAttributes[$attributeCode]);
}

$this->unlockAttribute();

public function unlockAttribute($attributeCode)
{
    if ($this->isLockedAttribute($attributeCode)) {
        unset($this->_lockedAttributes[$attributeCode]);
    }

    return $this;
}
$ids = this−>getResource()−>getCategoryIds(this->_getResource()->getCategoryIds(this−> 
g
​	
 etResource()−>getCategoryIds(this);
**
     * Retrieve product category identifiers
     *
     * @param Mage_Catalog_Model_Product $product
     * @return array
     */
public function getCategoryIds($product)
{
    $adapter = $this->_getReadAdapter();

    $select = $adapter->select()
        ->from($this->_productCategoryTable, 'category_id')
        ->where('product_id = ?', (int)$product->getId());

    return $adapter->fetchCol($select);
}

$this->lockAttribute();

/**
     * Lock attribute
     *
     * @param string $attributeCode
     * @return Mage_Catalog_Model_Abstract
     */
public function lockAttribute($attributeCode)
{
    $this->_lockedAttributes[$attributeCode] = true;
    return $this;
}

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论