Magento – Unable to access collection from custom model, returns null

custommodule

I have been trying to access 2 custom created tables which store additional values for a particular order, I am trying to implement a form that saves these details. I am trying to get the last id of the table and increment it by one.

/app/code/local/MyCompany/LinkCustomTable/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MyCompany_LinkCustomTable>
            <version>0.1.0</version>
        </MyCompany_LinkCustomTable>
    </modules>
    <global>
        <models>
            <mycompany_linkcustomtable>
                <class>MyCompany_LinkCustomTable_Model</class>
                <resourceModel>linkcustomtable_resource</resourceModel>
            </mycompany_linkcustomtable>

            <!-- model vs db table relation -->
            <linkcustomtable_resource>
                <class>MyCompany_LinkCustomTable_Model_Resource</class>
                <!-- db table with name pax and trip details -->
                <entities>
                    <paxdetails>
                        <table>mycompany_pax_details</table>
                    </paxdetails>
                    <tripdetails>
                        <table>mycompany_trip_details</table>
                    </tripdetails>

                </entities>
            </linkcustomtable_resource>
        </models>
        <resources>
            <linkcustomtable_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </linkcustomtable_write>
            <linkcustomtable_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </linkcustomtable_read>
        </resources>
    </global>
</config>

app/code/local/MyCompany/LinkCustomTable/Model/Tripdetails.php
(and app/code/local/MyCompany/LinkCustomTable/Model/Paxdetails.php is similar)

<?php
class MyCompany_LinkCustomTable_Model_Tripdetails extends Mage_Core_Model_Abstract
{

    protected function _construct()
    {   
    $this->_init('mycompany_linkcustomtable/tripdetails');
    }
}
?>

app/code/local/MyCompany/LinkCustomTable/Model/Resource/Tripdetails.php
(and app/code/local/MyCompany/LinkCustomTable/Model/Resource/Paxdetails.php is similar)

<?php
class MyCompany_LinkCustomTable_Model_Resource_Tripdetails extends Mage_Core_Model_Resource_Db_Abstract {
    protected function _construct()
    {
    $this->_init('mycompany_linkcustomtable/tripdetails', 'trip_id');
    }
}
?>

app/code/local/MyCompany/LinkCustomTable/Model/Resource/Tripdetails/Collection.php
(and app/code/local/MyCompany/LinkCustomTable/Model/Resource/Paxdetails/Collection.php is similar)

<?php
class MyCompany_LinkCustomTable_Model_Resource_Tripdetails_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
    protected function _construct()
    {
        $this->_init('mycompany_linkcustomtable/tripdetails');
    }
}
?>

But when i call the getCollection method on

Mage::getModel('mycompany_linkcustomtable/tripdetails') 

ie,

Mage::getModel('mycompany_linkcustomtable/tripdetails')->getCollection();  //->getLastItem()->getData('trip_id');

i get null value, tough there is a row in the mycompany_trip_details table.

I am sure that I am calling the right model because if I specify a different uri for getModel(), I get a error stating called getCollection() on boolean. I am not sure as to why I am unable get the collection from my custom tables. And if i uncomment the last part in the command

ie,

Mage::getModel('mycompany_linkcustomtable/tripdetails')->getCollection()->getLastItem()->getData('trip_id');

I get getLastItem() called on boolean (Mage::getModel('mycompany_linkcustomtable/tripdetails')->getCollection())

Best Answer

You have to fix your folder structure:

app/code/local/MyCompany/LinkCustomTable/Model/Tripcollection

and

app/code/local/MyCompany/LinkCustomTable/Model/Resource/Paxcollection

should be

app/code/local/MyCompany/LinkCustomTable/Model/Tripdetails

and

app/code/local/MyCompany/LinkCustomTable/Model/Resource/Paxdetails

according to name of your entities:

            <entities>
                <paxdetails>
                    <table>mycompany_pax_details</table>
                </paxdetails>
                <tripdetails>
                    <table>mycompany_trip_details</table>
                </tripdetails>

            </entities>