1

I have a custom module, called JLS_Composers, which is used to input a "composer." Each product can have one or more Composers associated with it (which is accomplished in the Product edit page). I now have a page which loops through all the products and displays a bit of information from each... which includes the composer(s). The problem is, the helper function in the JLS_Composer module is only returning the data for the first composer, and then repeats that for all subsequent products. I am getting the product collection via:

$products = Mage::getResourceModel('catalog/product_collection');
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('visibility', array('neq' => 1));
$products->addAttributeToFilter('status', 1);
$products->addAttributeToSort('composer', 'ASC');
$products->addAttributeToSort('name', 'ASC');
$products->addAttributeToSort('entity_id', 'ASC');
$products->load();

Then, within a foreach loop of ($products as $product), the helper is:

$composers = Mage::helper('jls_composers/product')->getSelectedComposers($product);

Do Magento helpers persist data somewhere? How is it that I am calling the helper each time, with a new, distinct product, but get the same results? I have never experienced this behavior and am curious to solve the problem. I have tried various methods of getting the information through the $product object but all to no avail. I can supply additional code as necessary.

Has anyone experienced and solved this? Or have I approached the issue in the wrong direction?

The Helper method is:

public function getSelectedComposers(Mage_Catalog_Model_Product $product){
    if (!$product->hasSelectedComposers()) {
        $composers = array();
        foreach ($this->getSelectedComposersCollection($product) as $composer) {
            $composers[] = $composer;
        }
        $product->setSelectedComposers($composers);
    }
    return $product->getData('selected_composers');
}

EDIT: The getSelectedComposersCollection method is as follows:

public function getSelectedComposersCollection(Mage_Catalog_Model_Product $product){
    $collection = Mage::getResourceSingleton('jls_composers/composer_collection')
        ->addProductFilter($product);
    return $collection;
}
2
  • getResourceSingleton try just getResource instead.
    – B00MER
    Sep 20 '14 at 16:59
  • 1
    There we go. getResource didn't work, but changing Mage::getResourceSingleton to Mage::getResourceModel worked great. I suppose the singleton should have been questionable to begin with.
    – KevinD
    Sep 22 '14 at 1:50
0

Nice first question Welcome to MagentoSE.

First are you sure there are multiple products being returned in your initial production collection?

zend_debug::dump($products->getData());

Next, you'll most likely want to setup some local protected $_product to your helper class and define a getter/setter:

/**
 * Set current Product
 *
 * @param Mage_Catalog_Model_Product $product
 * @return Mage_Catalog_Helper_Image
 */
protected function setProduct($product)
{
    $this->_product = $product;
    return $this;
}

/**
 * Get current Product
 *
 * @return Mage_Catalog_Model_Product
 */
protected function getProduct()
{
    return $this->_product;
}

As I believe Helpers in Magento are not as specific as Models.

For example:

Also be sure and review and ensure you're extending the abstract class Mage_Core_Helper_Abstract and make note there are caching calls as well, so you may also want to try and disable caching completely during testing.

On another note the helper method you've defined may make more sense as a Model and not a Helper. http://inchoo.net/magento/misuse-of-helper-classes-in-magento/ Hope this helps.

3
  • Yes, I checked that the returned products were unique by echoing their names. The helper class is extending Jls_Composers_Helper_Data, which in turn is extending Mage_Core_Helper_Abstract. Caching is also disabled on my dev store entirely. I'll add in the getter/setters and see if that helps and move everything over to a Model as well, but am still mystified as to why the issue was popping up to begin with. And thanks for the response!
    – KevinD
    Sep 19 '14 at 20:59
  • Can you post what your getSelectedComposersCollection method looks like in your helper?
    – B00MER
    Sep 20 '14 at 15:18
  • 1
    I've edited the question above to include the getSelectedComposersCollection method.
    – KevinD
    Sep 20 '14 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.