0

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())

1
  • Have you used magerun to test the class alias? You can also do it from a shell script – scrowler Apr 17 '16 at 19:53
2

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>
8
  • Sorry that was a typo actually "app/code/local/MyCompany/LinkCustomTable/Model/Tripcollection" was "app/code/local/MyCompany/LinkCustomTable/Model/Tripdetails " and "app/code/local/MyCompany/LinkCustomTable/Model/Resource/Tripcollection" was "app/code/local/MyCompany/LinkCustomTable/Model/Resource/Tripdetails" I have edited above accordingly. – Rakshith S Kumar Apr 17 '16 at 12:00
  • so your collection should be in Resource/Paxdetails/Collection.php. – Ilja Apr 17 '16 at 12:02
  • again, your collections should be in app/code/local/MyCompany/LinkCustomTable/Model/Resource/Tripdetails/Collection.php and class MyCompany_LinkCustomTable_Model_Resource_Tripdetails_Collection, same for Paxdetails – Ilja Apr 17 '16 at 12:08
  • I corrected the path and made it to app/code/local/MyCompany/LinkCustomTable/Model/Resource/Tripdetails/Collection and also changed the class but no use. – Rakshith S Kumar Apr 17 '16 at 12:24
  • 1
    Thanks a lot, it is working. Might be that was the cache, that was holding changes. Somebody please up vote this answer for me, Since I don't have enough reputation :( – Rakshith S Kumar Apr 18 '16 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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