Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento resource model for table with compound primary key

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

like image 584
shaune Avatar asked Jun 09 '10 18:06

shaune


People also ask

What is difference between model and resource model in Magento 2?

Models : Models are where your main business logic should be handled and is a single instance of an object. The model will use the resource model to talk to the database and get/set data for it on save() and load() . Resource Model : A resource model is where your main C.R.U.D happens (Create, Read, Update and delete).

What is declarative schema Magento 2?

Declarative Schema aims to simplify the Magento installation and upgrade processes. Previously, developers had to write database scripts in PHP for each new version of Magento. Various scripts were required for. Installing and upgrading the database schema. Installing and upgrading data.

What is use of resource model in Magento 2?

Every CRUD resource model in Magento must extends abstract class \Magento\Framework\Model\ResourceModel\Db\AbstractDb which contain the functions for fetching information from database. Like model class, this resource model class will have required method _construct() .


1 Answers

Like most Active Record inspired models, Magento's Mage_Core_Model_Abstract wasn't built with support for composite primary keys in mind. Any models that inherit from this base (meaning all of them) inherit this assumption. If you want to use composite primary keys, you won't be able to. Your choices are to go the Magento Model route and create a single primary key ("fake", as you called it) and then apply a unique index to the table, OR implement you own Model layer using the base Zend DB Table, OR import a third party model solution into the system that supports the features you want.

As far as Zend Framework goes, the Magento team used Zend's Table Gateway Pattern to implement an Active Record style Model layer for their framework. Zend Framework isn't an application stack like Cake or Rails, it's a collection of class libraries that can be used to build application stacks (or applications, or lots of other things). Just because something is supported in the Zend Framework classes doesn't mean that systems and applications using the Zend Framework get it for free.

like image 64
Alan Storm Avatar answered Oct 14 '22 11:10

Alan Storm