Magento resource model for table with compound primary key

Posted by sdek on Stack Overflow See other posts from Stack Overflow or by sdek
Published on 2010-06-09T18:48:59Z Indexed on 2010/06/09 18:52 UTC
Read the original article Hit count: 419

I am creating a custom module for a Magento ecommerce site, and the module will center around a new (ie, custom) table that has a compound/composite primary key, or rather the table has two columns that make up the primary key. Does anybody know how to create your models/resource models based on a table with a compound key?

To give a few more details, I have looked up several tutorials and also used the excellent moduleCreator script. But it seems like all the tutorials revolve around the table having a PK with just one column in it. Something like this:

class <Namespace>_<Module>_Model_Mysql4_<Module> extends Mage_Core_Model_Mysql4_Abstract {
   public function _construct(){
        $this->_init('<module_alias>/<table_alias>', '<table_primary_key_id>');
   }
} 

Also, I just noticed that looking at the database model almost all tables have a single primary key. I understand this has much to do with the EAV-style db structure, but still is it possible to use a table with a compound PK? I want to stick with the Magento framework/conventions if possible. Is it discouraged? Should I just change the structure of my custom table to have some dummy id column? I do have the ability to do that, but geez!

(Another side note that I thought I would mention is that it looks like the Zend Framework provides a way to base a class on a table with compound primary key (see Example #20 on this page - about half-way down), so it seems that the Magento framework should also provide for it... I just don't see how.)

© Stack Overflow or respective owner

Related posts about zend-framework

Related posts about data-structures