0

I'm trying to create my first Magento module, and I have a strange error. I have this method in my model (it extends Mage_Core_Model_Abstract) :

public function getAllProducts() { 
        $products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->setOrder('entity_id', 'DESC')->setPageSize(5);
        return $products;
    }

And I'm calling this method from a block:

public function getAllProducts() {

    $arrayOfProducts = array(); 
    $products = Mage::getModel('recentproducts/recentproducts')->getAllProducts();

    foreach ($products as $product) {
      var_dump($product);
      $arrayOfProducts[] = array('id' => $product­->getId(),'name' => $product­->getName(),'url' => $product­->getProductUrl());
    }

    return $arrayOfProducts;
  }

On my phtml, when doing this :

<?php
$products = $this->getAllProducts();
?>

I have Fatal error: Call to a member function getId() on null in...`

getId()is a product getter function, and when var_dump($product) I have all the product data printed . It's the same problem with getName() and all getter functions. What could be the problem ?

0

Nothing is wrong with this particular snippet. Verify what file and line of code is mentioned in error message. Seems like the error occurs not in code part you mentioned.

| improve this answer | |
  • it's on this line $arrayOfProducts[] =... where the getId() is. – androniennn Dec 15 '16 at 22:47

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.