Magento 1.6 Collection – Fatal Error: Call to a Member Function getCollection() on a Non-object

collection;magento-1.6

I am trying to get a collection of objects of a table of our database. Unfortunately the following code was leading to an error.

my code is

  <?php

class Ics_EasyLife_Block_Template extends Mage_Core_Block_Template
{
    public function methodblock()
    {

        //on initialize la variable
        $retour='';

        $collection = Mage::getModel('easylife/easylife')->getCollection()->setOrder('id_ics_easylife','asc');

        foreach($collection as $data)
        {
            $retour .= $data->getData('nom').' '.$data->getData('prenom').' '.$data->getData('telephone').'<br />';
        }

        Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!');
        return $retour;
    }
}

//config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Ics_EasyLife>
            <version>0.0.1</version>
        </Ics_EasyLife>
    </modules>
    <frontend>
        <routers>
            <icscontroller>
                <use>standard</use>
                <args>
                    <module>Ics_EasyLife</module>
                    <frontName>display</frontName>
                </args>
            </icscontroller>
        </routers>
        <layout>
            <updates>
                <easylife>
                    <file>easylife.xml</file>
                </easylife>
            </updates>
        </layout>
    </frontend>
    <global>
        <blocks>
            <easylife>
                <class>Ics_EasyLife_Block</class>
            </easylife>
        </blocks>
        <models>
            <easylife>
                <class>Ics_EasyLife_Model</class>
                <resourceModel>easylife_resource</resourceModel>
            </easylife>
            <easylife_resource>
                <class>Ics_EasyLife_Model_Resource</class>
                <entities>
                    <easylife>
                        <table>ics_test</table>
                    </easylife>
                </entities>
            </easylife_resource>
        </models>
        <resources>
            <easylife_write>
                <connection>core_write</connection>
            </easylife_write>
            <easylife_read>
                <connection>core_read</connection>
            </easylife_read>
        </resources>
    </global>
</config>

//Model->Easylife.php

<?php

class Ics_EasyLife_Model_Easylife extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('easylife/easylife');
    }
}

//Model->Resource->Easylife.php

<?php
class Ics_EasyLife_Model_Resource_Easylife extends Mage_Core_Model_Resource_Db_Abstract
{
    public function _construct()
    {
        $this->_init('easylife/easylife','id_easylife');
    }
}

?>

//Model->Resource->Easylife->Collection.php

<?php
class Ics_EasyLife_Model_Resource_Easylife_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('easylife/easylife');
    }
}

//sql->install-0.0.1.php

<?php


$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();

$installer->run("

--DROP TABLE IF EXISTS {$this->getTable('easylife')};
CREATE TABLE {$this->getTable('easylife')} (
'id_easylife' INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
'nom' VARCHAR( 50 ) NOT NULL ,
'prenom' VARCHAR( 50 ) NOT NULL ,
'telephone' VARCHAR( 20 ) NOT NULL,
 PRIMARY KEY ('id_easylife')
);ENGINE=InnoDB DEFAULT CHARSET=utf8;

");

$installer->endSetup();

can anyone tell me where I went wrong?

thanks in advance.

Best Answer

The error basically tells us that Mage::getModel('easylife/easylife') doesn't return a valid class. Please make sure the file Ics/Easylife/Model/Easylife.php exists and looks somewhat like the code posted below, this should give you access to the resource model.

class Ics_Easylife_Model_Easylife extends Mage_Core_Model_Abstract
{
   public function _construct()
   {
      parent::_construct();
      $this->_init('easylife/easylife');
   }
}

If that doesn't work I'd advice you to check out this wikipage which clearly outlines what you need to do on using collections.