1

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');
     }
}

2 Answers 2

4

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>

9
  • I've tried your solution to change 'lesson3' on my block and models, but I find the error like this Fatal error: Uncaught Error: Call to a member function setOrder() on boolean
    – gufran
    Commented May 20, 2016 at 5:34
  • i have table 'celebes' and structur is like this : id_celebes <int> name <varchar(50)> surename <varchar(50)> phone <varchar(20)> and my table is empty
    – gufran
    Commented May 20, 2016 at 5:52
  • i have edit model Collection.php from Celebes_Lesson3_Test_Model_Mysql4_Test_Collection to Lesson3_Test_Model_Mysql4_Test_Collection but i still find error "setOrder() on boolean"
    – gufran
    Commented May 20, 2016 at 6:21
  • if i delete "->setOrder('id_celebes','asc');", a page is run but data of 'celebes' is did not show
    – gufran
    Commented May 20, 2016 at 6:24
  • 1
    that happens because ->getCollection() returns null for some reason. I'm looking on it. It has to be a configuration issue though.
    – Marius
    Commented May 20, 2016 at 6:25
0

I found the problem on config <resourceModel>Test_mysql4</resourceModel>.

I change it with this <resourceModel>test_mysql4</resourceModel> and its work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.