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 ?