1

enter image description here

app/code/local/Info/news/Bock/Adminhtml/news/Grid.php

 <?php
    
    class Info_News_Block_Adminhtml_News_Grid extends Mage_Adminhtml_Block_Widget_Grid{
        public function __construct(){
            parent::__construct();
            $this->setId('newsGridView');
            $this->setDefaultSort('news_id');
            $this->setDefaultDir('DESC');
            $this->setSaveParametersInSession(true);
        }
        protected function _prepareCollection(){
            //$collection=new Varien_Data_Collection();
           $collection = Mage::getModel('news/news')->getCollection();
            $this->setCollection($collection);
            return parent::_prepareCollection();
        }
    
    
        protected function _prepareColumns(){
            $this->addColumn("news_id",array(
                "header"=>Mage::helper("news")->__("ID"),
                "type"=>"number",
                "index"=>"news_id"
            ));
            $this->addColumn("news_title",array(
                "header"=>Mage::helper("news")->__("Title"),
                "type"=>"text",
                "index"=>"news_title"
            ));        $this->addColumn("news_content",array(
                "header"=>Mage::helper("news")->__("Content"),
                "type"=>"text",
                "index"=>"news_content"
            ));
        }
    }

app/code/local/Info/News/Model/News.php

<?php

class Info_News_Model_News extends Mage_Core_Model_Abstract{
    protected function _construct() {
        $this->_init('news/news');
    }
}

app/code/local/Info/News/Model/Mysql4/News.php

<?php

class Info_News_Model_Mysql4_News extends Mage_Core_Model_Mysql4_Abstract{
    protected function _construct() {
        $this->_init('news/news','news_id');
    }
}

app/code/local/Info/News/Model/Mysql4/News/Collection.php <?php

class Info_News_Model_Mysql4_News_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract{
    public function _construct() {
        parent::_construct();
        $this->_init('news/news');
    }
}

app/code/local/Info/News/Block/Adminhtml/News.php

<?php
class Info_News_Block_Adminhtml_News extends Mage_Adminhtml_Block_Widget_Grid_Container {
    public function _construct (){
       
        $this->_controller = 'adminhtml_news';
        $this->_blockGroup = 'news';
        $this->_headerText = Mage::helper('news')->__("Manage News");
        parent::_construct();
    }
}

app/code/local/Info/News/etc/config.xml

  <config>
        <modules>
            <Info_News>
                <version>
                    0.0.1
                </version>
            </Info_News>
        </modules>
        <admin>
            <routers>
                <news>
                <use>admin</use>
                <args>
                <module>Info_News</module>
                <frontName>admin_news</frontName>
                </args>
                </news>
            </routers>
        </admin>
        <adminhtml>
            <layout>
                <updates>
                    <news>
                        <file>info/news.xml</file>
                    </news>
                </updates>
            </layout>
        </adminhtml>
        <global>
            <models>
                <news>
                    <class>
                        Info_News_Model
                    </class>
                    <resourceModel>
                        news_mysql4
                    </resourceModel>
                </news>
                <news_mysql4>
                    <class>
                        Info_News_Model_Mysql4
                    </class>
                    <entities>
                        <news>
                            <table>
                                info_news
                            </table>
                        </news>
                    </entities>
                </news_mysql4>
            </models>
            <helpers>
                <news>
                    <class>
                        Info_News_Helper
                    </class>
                </news>
            </helpers>
            <blocks>
                <news>
                    <class>
                        Info_News_Block
                    </class>
                </news>
            </blocks>
            <resources>
                <news_setup>
                    <setup>
                        <module>
                            Info_News
                        </module>
                    </setup>
                    <connection>
                        <use>
                            core_setup
                        </use>
                    </connection>
                </news_setup>
                <news_write>
                    <connection>
                        <use>
                            core_write
                        </use>
                    </connection>
                </news_write>
                <news_read>
                    <connection>
                        <use>
                            core_read
                        </use>
                    </connection>
                </news_read>
            </resources>
        </global>
    </config>

app/design/adminhtml/default/default/layout/info/news.xml

<?xml version="1.0"?>
<layout version="0.0.1">
    <news_adminhtml_news_index>
        <reference name="content">
            <block type="news/adminhtml_news" name="news" />
        </reference>
    </news_adminhtml_news_index>
</layout>

This is my result

enter image description here

2
  • MySql4-classes are deprecated, better use Resource-classes
    – sv3n
    Commented Nov 16, 2022 at 22:35
  • show me how ? please Commented Nov 18, 2022 at 22:02

3 Answers 3

0

In app/code/local/Info/News/Block/Adminhtml/News.php, you should declare __construct() method (it contains 2 underscores) instead of _construct() method (it contain 1 underscore).

<?php
class Info_News_Block_Adminhtml_News extends Mage_Adminhtml_Block_Widget_Grid_Container {
    public function __construct (){
       
        $this->_controller = 'adminhtml_news';
        $this->_blockGroup = 'news';
        $this->_headerText = Mage::helper('news')->__("Manage News");
        parent::__construct();
    }
}

Updated: Found your issues:

Your app/code/local/Info/News/etc/config.xml file has incorrect config. Xml nodes value for version, models, and resources must not contain any space nor new line.

And because Mysql4 classes are deprecated, you should use Resource classes instead (It does not relate to your issue: can't get model).

The implementation:

Step 1: In app/code/local/Info/News/etc/config.xml:

  1. Change version tag to <version>0.0.1</version>.

  2. Change your code for models tag to the following:

<models>
    <news>
        <class>Info_News_Model</class>
        <resourceModel>news_resource</resourceModel>
    </news>
    <news_resource>
        <class>Info_News_Model_Resource</class>
        <entities>
            <news>
                <table>info_news</table>
            </news>
        </entities>
    </news_resource>
</models>
  1. Change your code for resource to the following:
<resources>
    <news_setup>
        <setup>
            <module>Info_News</module>
        </setup>
    </news_setup>
    <news_write>
        <connection>
            <use>core_write</use>
        </connection>
    </news_write>
    <news_read>
        <connection>
            <use>core_read</use>
        </connection>
    </news_read>
</resources>

Finally, your app/code/local/Info/News/etc/config.xml should be changed like the following:

<config>
    <modules>
        <Info_News>
            <version>0.0.1</version>
        </Info_News>
    </modules>
    <admin>
        <routers>
            <news>
                <use>admin</use>
                <args>
                    <module>Info_News</module>
                    <frontName>admin_news</frontName>
                </args>
            </news>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <news>
                    <file>info/news.xml</file>
                </news>
            </updates>
        </layout>
    </adminhtml>
    <global>
        <models>
            <news>
                <class>Info_News_Model</class>
                <resourceModel>news_resource</resourceModel>
            </news>
            <news_resource>
                <class>Info_News_Model_Resource</class>
                <entities>
                    <news>
                        <table>info_news</table>
                    </news>
                </entities>
            </news_resource>
        </models>
        <helpers>
            <news>
                <class>Info_News_Helper</class>
            </news>
        </helpers>
        <blocks>
            <news>
                <class>Info_News_Block</class>
            </news>
        </blocks>
        <resources>
            <news_setup>
                <setup>
                    <module>Info_News</module>
                </setup>
            </news_setup>
            <news_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </news_write>
            <news_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </news_read>
        </resources>
    </global>
</config>

Step 2: Remove app/code/local/Info/News/Model/Mysql4 folder.

Step 3: Create app/code/local/Info/News/Model/Resource/News.php file

<?php

class Info_News_Model_Resource_News extends Mage_Core_Model_Resource_Db_Abstract
{
    protected function _construct() {
        $this->_init('news/news','news_id');
    }
}

Step 4: Create app/code/local/Info/News/Model/Resource/News/Collection.php file

<?php

class Info_News_Model_Resource_News_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    public function _construct() {
        $this->_init('news/news');
    }
}

Done, now you can go the admin to see result.

22
  • It doesn't work !! Commented Nov 17, 2022 at 10:58
  • Let's change $this->setId('newsGridView'); to $this->setId('newsGrid'); in app/code/local/Info/news/Bock/Adminhtml/News/Grid.php and update app/code/local/Info/News/Block/Adminhtml/News.php file as my updated answer and clear the cache.
    – Tu Van
    Commented Nov 17, 2022 at 16:42
  • And <layout version="0.0.1"> should be changed to <layout version="0.1.0"> in app/design/adminhtml/default/default/layout/info/news.xml
    – Tu Van
    Commented Nov 17, 2022 at 17:38
  • I did all your suggestions, but still not working I can't progress since last Friday, please help Commented Nov 17, 2022 at 19:59
  • Let's zip your module and upload somewhere then send the link here, I'll check.
    – Tu Van
    Commented Nov 17, 2022 at 20:35
1

I think error comes from router and/or layout file.

config.xml

<config>
    <modules>
        <Info_News>
            <version>0.0.1</version>
        </Info_News>
    </modules>
    <global>
        <helpers>
            <news>
                <class>Info_News_Helper</class>
            </news>
        </helpers>
        <blocks>
            <news>
                <class>Info_News_Block</class>
            </news>
        </blocks>
        <models>
            <news>
                <class>Info_News_Model</class>
                <resourceModel>news_resource</resourceModel>
            </news>
            <news_resource>
                <class>Info_News_Model_Resource</class>
                <entities>
                    <news>
                        <table>info_news</table>
                    </news>
                </entities>
            </news_resource>
        </models>
        <resources>
            <news_setup>
                <setup>
                    <module>Info_News</module>
                </setup>
            </news_setup>
        </resources>
    </global>
    <admin>
        <!-- fixed admin router -->
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <news before="Mage_Adminhtml">Info_News_Adminhtml</news>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <news>
                    <file>info/news.xml</file>
                </news>
            </updates>
        </layout>
    </adminhtml>
</config>

news.xml

    <adminhtml_news_index>
        <reference name="content">
            <block type="news/adminhtml_news" name="news">
                <block type="news/adminhtml_news_grid" name="news.grid" as="grid"/>
            </block>
        </reference>
    </adminhtml_news_index>
  • rename install script to install-0.0.1.php
  • set path in adminhtml.xml to <action>adminhtml/news/index</action>
  • in block classes use _construct() (one underscore)
  • class Info_News_Model_Resource_News extends Mage_Core_Model_Resource_Db_Abstract
  • class Info_News_Model_Resource_News_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
7
  • 1
    Thanks, i've found the solution Commented Nov 20, 2022 at 14:23
  • @abderrahimrida you should also fix the router ...
    – sv3n
    Commented Nov 20, 2022 at 14:50
  • @sv3n his router works fine, don't need to fix. I'll write more detail after answer you question in my answer.
    – Tu Van
    Commented Nov 20, 2022 at 14:51
  • The config router in the original poster will get URL: base_url/admin/news/index while the config router as your share is base_url/adminhtml/news/index
    – Tu Van
    Commented Nov 20, 2022 at 15:35
  • 1
    @abderrahimrida you should update the router as sv3n advised to make your code safe. "Magento released Security Patch SUPEE 6788 on Oct 27, 2015, to ensure protection against threats such as remote code execution, information leaks, and cross-site scripting."
    – Tu Van
    Commented Nov 21, 2022 at 1:55
0

enter image description here

look, when i check with this tool , the model has worked

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.