Magento 1.9 Error – Fatal Error: Uncaught Error: Call to a Member Function getCollection() on Boolean

collection;errormagento-1.9php-7

i have problem when create model databse
Lesson3/Test/Block/Monblock.php

<?php
class Lesson3_Test_Block_Monblock extends Mage_Core_Block_Template
{
    public function methodblock()
    {
        $retour='';
        /* we are doing the query to select all elements of the pfay_test table (thanks to our model test/test and we sort them by id */
        $collection = Mage::getModel('test/test')->getCollection()->setOrder('id_celebes','asc');
        /* then, we check the result of the query and with the function getData() */
        foreach($collection as $data)
        {
             $retour .= $data->getData('name').' '.$data->getData('surename').' '.$data->getData('phone').'<br />';
        }
        //i return a success message to the user thanks to the Session.
        Mage::getSingleton('adminhtml/session')->addSuccess('Congratulation !!');
        return $retour;
     }
}

Lesson3/Test/etc/config.xml

<?xml version="1.0"?>
  <config>
     <modules>
        <Lesson3_Test>
          <version>1.0.0</version>
        </Lesson3_Test>
     </modules>
     <frontend>
       <routers>
          <lesson3>
              <use>standard</use>
              <args>
                 <module>Lesson3_Test</module>
                 <frontName>lesson3</frontName>
              </args>
           </lesson3>
       </routers>
       <layout>
         <updates>
              <lesson3>
                   <file>lesson3.xml</file>
               </lesson3>
          </updates>
      </layout>
    </frontend>
    <global>
      <blocks>
        <lesson3>
          <class>Lesson3_Test_Block</class>
        </lesson3>
      </blocks>
    <models>
        <lesson3>
            <class>Lesson3_Test_Model</class>
            <resourceModel>Test_mysql4</resourceModel>
        </lesson3>
        <lesson3_mysql4>
            <class>Lesson3_Test_Model_Mysql4</class>
            <entities>
                <test>
                    <table>celebes</table>
                </test>
            </entities>
        </lesson3_mysql4>
    </models>
        <!-- allow the plugin to read and write -->
    <resources>
        <!-- connection to write -->
        <lesson3_write>
            <connection>
                <use>core_write</use>
            </connection>
        </lesson3_write>
        <!-- connection to read -->
       <lesson3_read>
          <connection>
             <use>core_read</use>
          </connection>
       </lesson3_read>
    </resources>
    </global>
</config>

Lesson3/Test/controllers/IndexController.php

<?php

class Lesson3_Test_IndexController extends Mage_Core_Controller_Front_Action
{
   public function indexAction()
   {
        $this->loadLayout();
        $this->renderLayout();
        Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles());
   }

    public function testAction()
    {
        echo 'test mymethod';
    }
}

Lesson3/Test/Model/Test.php

<?php
class Lesson3_Test_Model_Test extends Mage_Core_Model_Abstract
{
     public function _construct()
     {
         parent::_construct();
         $this->_init('test/test');
     }
}

Lesson3/Test/Model/Mysql4/Test.php

<?php
class Lesson3_Test_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract
{
     public function _construct()
     {
         $this->_init('test/test', 'id_celebes');
     }
}

Lesson3/Test/Model/Mysql4/Test/Collection.php

<?php
class Celebes_Lesson3_Test_Model_Mysql4_Test_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
 {
     public function _construct()
     {
         parent::_construct();
         $this->_init('test/test');
     }
}

Best Answer

you declared your models like this:

 <models>
    <lesson3>
        <class>Lesson3_Test_Model</class>
        <resourceModel>Test_mysql4</resourceModel>
    </lesson3>
    <lesson3_mysql4>
        <class>Lesson3_Test_Model_Mysql4</class>
        <entities>
            <test>
                <table>celebes</table>
            </test>
        </entities>
    </lesson3_mysql4>
</models>

this means the alias for the models is lesson3 (the first tag under <models>).
So you need to instantiate your models like this:

Mage::getModel('lesson3/model_name_here'); 

In your case, instead of

$collection = Mage::getModel('test/test')->getCollection()....

you need to use

$collection = Mage::getModel('lesson3/test')->getCollection()....

Also replace in Lesson3_Test_Model_Mysql4_Test this

$this->_init('test/test', 'id_celebes');

with this

$this->_init('lesson3/test', 'id_celebes');

and in Celebes_Lesson3_Test_Model_Mysql4_Test_Collection and Lesson3_Test_Model_Test this

$this->_init('test/test');

with this

$this->_init('lesson3/test');

Or, instead of all the above you can replace in config.xml the tag <lesson3> under <models> and <blocks> with <test>.

[EDIT]

<resourceModel>Test_mysql4</resourceModel> should be <resourceModel>lesson3_mysql4</resourceModel>

Related Topic