Magento – Fatal error: Call to a member function getCollection() on a non-object

extensionsmagento-1.9magento-community

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

Structure:
files structure

my code is

Bucons/ProductShip/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
    <Bucons_ProductShip>
        <version>0.1.3</version>
    </Bucons_ProductShip>
</modules>
<frontend>
    <routers>
        <productship>
            <use>standard</use>
            <args>
                <module>Bucons_ProductShip</module>
                <frontName>productship</frontName>
            </args>
        </productship>
    </routers>
</frontend>
<global>
    <models>
        <product_ship>
            <class>Bucons_ProductShip_Model</class>
            <resourceModel>product_ship_resource</resourceModel>
        </product_ship>
        <product_ship_resource>
            <class>Bucons_ProductShip_Model_Resource</class>
            <entities>
                <buycentropeships>
                    <table>buycentrope_ships</table>
                </buycentropeships>
            </entities>
        </product_ship_resource>
    </models>
    <resources>
        <productship_setup>
            <setup>
                <module>Bucons_ProductShip</module>
                <class>Mage_Customer_Model_Entity_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </productship_setup>
        <productship_write>
            <connection>
                <use>core_write</use>
            </connection>
        </productship_write>
        <productship_read>
            <connection>
                <use>core_read</use>
            </connection>
        </productship_read>
    </resources>
</global>
</config>

Bucons/ProductShip/sql/mysql4-install-0.1.0.php

<?php

$installer = $this;

$installer->startSetup();

$installer->run("

DROP TABLE IF EXISTS {$this->getTable('buycentrope_ships')};
CREATE TABLE {$this->getTable('buycentrope_ships')} (
  `id` int(11) unsigned NOT NULL auto_increment,
  `from` varchar(255) NOT NULL default '',
  `to` varchar(255) NOT NULL default '',
  `date` date NULL,
  `time` time NULL,
  `created_time` datetime NULL,
  `update_time` datetime NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    ");

  $installer->endSetup();

Bucons/ProductShip/controllers/IndexController.php

<?php
class Bucons_ProductShip_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "test";
        $ships = Mage::getModel('product_ship/buycentropeships')->getCollection()->getData();
        var_dump($ships);
        $this->loadLayout();     
        $this->renderLayout();
    }
}

Bucons/ProductShip/Model/Buycentropeships.php

class Bucons_ProductShip_Model_Buycenropeships extends Mage_Core_Model_Abstract {

    protected function _construct() {
        $this->_init('product_ship/buycentropeships');
    }

}

Bucons/ProductShip/Model/Resource/Buycentropeships.php

<?php

class Bucons_ProductShip_Model_Resource_Buycenropeships extends Mage_Core_Model_Resource_Db_Abstract {
    protected function _construct() {
        $this->_init('product_ship/buycentropeships', 'id');
    }
}

Bucons/ProductShip/Model/Resource/Buycentropeships/Collection.php

<?php
class Bucons_ProductShip_Model_Resource_Buycenropeships_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {

    protected function _construct()
    {
            $this->_init('product_ship/buycentropeships');
    }
}

app/etc/modules/Bucons_ProductShip.php

<?xml version="1.0"?>
<config>
    <modules>
        <Bucons_ProductShip>
        <active>true</active>
        <codePool>local</codePool>
        </Bucons_ProductShip>
    </modules>
</config>

can anyone tell me where I went wrong?
I see the result of module on http://complit2.bucons.com/buycentrope/productship/index

thanks in advance.

Best Answer

The _construct method from your model (Bucons_ProductShip_Model_Buycenropeships) should look like this

public function _construct()
{
    parent::_construct();
    $this->_init('product_ship/buycentropeships');
}

also the one from the collection resource model should look like this

protected function _construct()
{
    parent::_construct();
    $this->_init('product_ship/buycentropeships');
}