Magento – How to access custom table/attributes using the ORM

Architecturecustomdatabasemodel

I've been struggling with this for a while – I'm new to Magento and haven't programmed in PHP for a while, so please, bear with me.

Doing some changes on a Shipping module to magento I've come to need to save and query an specific extra attribute, which I've managed to add through a simple update script:

<?php

$installer->startSetup();
$table = new Varien_Db_Ddl_Table(); $table->setName('shipping_token');

$table
->addColumn('token_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, 
    array(
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        'identity'  => true,
    ), 'Entity Id')
->addColumn('rate_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null,
    array(
        'unsigned'  => true,
        'nullable'  => false,
    ), 'Quote Id')
->addColumn('token', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, 
    array(
        'unique'    => true,
    ), 'Quote Token')
->addIndex(
    'ID_QUOTE_TOKEN',
    'token_id')
->addForeignKey(
    'FK_TOKEN',
    'rate_id',
    'sales_flat_quote_shipping_rate', 
    'rate_id', 
    Varien_Db_Ddl_Table::ACTION_CASCADE,
    Varien_Db_Ddl_Table::ACTION_CASCADE);

 $installer->getConnection()->createTable($table);
 $installer->endSetup();

After that, with some ups and downs, I've been following this tutorial trying to access the models with:

 $shipping = Mage::getModel('shipping/token');
 $shipping->setToken("whatevercomes");

or

 $shipping = Mage::getModel('shipping/token');
 $shipping->setData('token', 'whatevercomes');

but since I'm doing this when I receive the shipping rates, hitting

 $shipping->save();

not only doesn't work but doesn't throw any exceptions either. So, am I doing anything blatantly wrong, or is there any way to dig deeper on the debugging of the matter? I'm lost here.

Best Answer

Let's take it a step at a time.

First, ensure that your table has been created by checking the database.

Once that's been verified, start logging your queries to see if there are any problems. You can do this by opening up the file in lib/Varien/Db/Adapter/Pdo/Mysql.php. In that class, you will find a number of protected variables that allow you to log queries. In this example, we will set $_logAllQueries to true. You can now go into var/debug/pdo_mysql.log and check against all queries that are executed.

Also make sure you have logging enabled on your site. In the Magento admin, go to System->Configuration->Advanced->Developer->Log Settings. Set Enabled to "Yes".

As you work with the ORM, you can always debug your output. Try:

Mage::log($shipping->debug()); // for logging your data
Zend_Debug::dump($shipping->debug()); // for printing it on the screen

Once that's done, make sure you are setting all values required by your table. Does the foreign key you've set exist yet? How are you assigning it to the table you've created when you save your values?

Should your method look more like this?

$shipping->setToken('whatevercomes')->setRateId('shippingRateIdHere')->save();

Once you have some logging or debug output, that can help you better understand what's going on with your error.

I hope this helps!