3

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.

  • 1
    Can you also post part of your config.xml involved with the (resource) models and maybe your directory structure. The error basically tells us that Mage::getModel('easylife/easylife') doesn't return a valid class – Sander Mangel Oct 1 '13 at 9:33
  • hi sander,see updated my code – Manoj Kumar Oct 1 '13 at 9:35
  • 1
    Quick question, does the file Ics/Easylife/Model/Easylife.php actually exist? – Sander Mangel Oct 1 '13 at 9:36
  • ok I updated but it's returning same error. – Manoj Kumar Oct 1 '13 at 9:38
  • that might b the problem, getModel is not getting the model. – Shathish Oct 1 '13 at 9:45
6

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.

  • hi sander, see my update code, it's given error. Could you provide solution? – Manoj Kumar Oct 1 '13 at 14:23
  • Please use the wikipage I've provided in my answer to build your extension. I've used it before and it works, I'm 100% sure of that. Also it's best to post your error in the question. – Sander Mangel Oct 1 '13 at 14:26
3

Check your module's Model properly there must something wrong. do you have properly structured collection class in your module's model ?

  • hi sarvagya, see my update code, it's given error. Could you provide solution? – Manoj Kumar Oct 1 '13 at 14:24
  • 1
    Just before the line $collection = Mage::getModel('easylife/easylife')->getCollection()->setOrder('id_ics_easylife','asc'); put this and check it prints your model class name correctly. echo get_class(Mage::getModel('easylife/easylife'));exit; – Sukeshini Oct 1 '13 at 17:23
  • try Su123 method you will be able to know your class exists or not. – Sarvagya Oct 1 '13 at 19:42
  • I create my own module and call var_dump(get_class(Mage::getModel('fastsearchhint/product')));exit; but it return false – Abdus Salam Nov 5 '15 at 6:19
2

Sometime if you try to un-insall any extension (for me its was 'Mgt_DeveloperToolbar') by manually delete all related files but still you need to clear data from 'core_resource' table.

  • core_resource table have entries for the modules which have setup resources so whenever you install any extension it checks inside the core_resource if it is already present. If you are re-installing extension you will get issue if the same extension with same version is already present inside core_resource table. – Sarvagya Apr 28 '17 at 11:39
1

In case you try to un-insall any extension by manually delete all, need to clear all cache. If you are using redis cache, please try in command line:

Nguyens-MacBook-Pro:~ nguyende$ redis-cli flushall

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.