【问题标题】:Magento collection iterator - cannot get additional attributeMagento 集合迭代器 - 无法获取附加属性
【发布时间】:2014-08-28 04:02:06
【问题描述】:

当我使用资源迭代器时,我无法从产品集合中获取附加属性“名称”。

我的产品系列:

        $productCollection = Mage::getModel('catalog/product')->getCollection();
        $productCollection->addAttributeToSelect('name')
        ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left')
        ->addAttributeToFilter('category_id', array('in' => $subCategories))->addAttributeToFilter('visibility', '4')
        ->getSelect()->group('e.entity_id');

迭代器:

Mage::getSingleton('core/resource_iterator')->walk($productCollection->getSelect(), array(array($this, 'generateXml')));

generateXml 函数:

public function generateXml($args){
    var_dump($args['row']);
...

array(11) {
  ["entity_id"]=>
  string(5) "49335"
  ["entity_type_id"]=>
  string(1) "4"
  ["attribute_set_id"]=>
  string(2) "18"
  ["type_id"]=>
  string(6) "simple"
  ["sku"]=>
  NULL
  ["has_options"]=>
  string(1) "0"
  ["required_options"]=>
  string(1) "0"
  ["created_at"]=>
  string(19) "2014-05-28 19:18:49"
  ["updated_at"]=>
  string(19) "2014-05-28 19:20:21"
  ["category_id"]=>
  string(3) "236"
  ["visibility"]=>
  string(1) "4"
}

提前致谢。

【问题讨论】:

    标签: magento magento-1.8


    【解决方案1】:

    改为:

    (...)
    $productCollection->addAttributeToSelect('name')
    (...)
    

    应该是:

    (...)
    $productCollection->addAttributeToSelect(array('name'),'inner')
    (...)
    

    【讨论】:

    • 终于找到了!到目前为止,我发现的所有示例都建议 addAttributeToSelect('name') 或 addAttributeToSelect(array('name')) 根本不起作用。添加连接类型为我解决了这个问题。应该注意不要以这种方式添加太多连接,否则性能可能会受到严重影响:-)
    • 快速旁注:第二个参数是 $jointype,有效值似乎是 false、“inner”或“left”。
    【解决方案2】:

    我不完全确定您想要完成什么。但是,以下内容可能会对您有所帮助。即使,我不熟悉 resource_interator 这可能会有所帮助

    <?php
    // $this-> may not work depending upon block type. However this helper is what allows you to grab other necessary values     
    $_helper = $this->helper('catalog/output');  
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        // ->addCategoryFilter($category) If you have the category object you can filter by this
        // I'm just using the below example to grab these three attribute values. 
        ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
        ->load();
    ?>
    
                <!-- Display Each product's detailed info  Granted this is not in an xml format.  Which you can alter to your needs,  but this should be a good base point I don't believe the resource  -->
            <?php foreach ($products as $product): ?>
                <li>
                <?php // Product Image ?>
                <a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" /></a>
                <?php // Product description ?>
                <?php $_productNameStripped = $this->stripTags($product->getName(), null, true); ?>
    
                <!-- Below is where the helper is needed for product name.  -->
                <h3 class="product-name"><a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $productNameStripped; ?>"><?php echo $_helper->productAttribute($product, $product->getName() , 'name'); ?></a></h3>
                </li>
            <?php endforeach ; ?>
    

    【讨论】:

    • 非常感谢您的帮助。我会避免使用此解决方案,因为在这种情况下,我正在处理大型集合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2011-11-08
    • 2016-12-06
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多