简体   繁体   English

具有复合主键的表的Magento资源模型

[英]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. 我正在为Magento电子商务网站创建一个自定义模块,该模块将围绕一个具有复合/复合主键的新(即自定义)表,或者说该表有两列构成主键。 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. 为了提供更多细节,我查阅了几个教程并使用了优秀的moduleCreator脚本。 But it seems like all the tutorials revolve around the table having a PK with just one column in it. 但似乎所有的教程围绕着一个只有一列的PK的表。 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? 我知道这与EAV风格的数据库结构有很大关系,但仍然可以使用带有复合PK的表吗? I want to stick with the Magento framework/conventions if possible. 如果可能的话,我想坚持使用Magento框架/惯例。 Is it discouraged? 气馁了吗? Should I just change the structure of my custom table to have some dummy id column? 我应该只更改自定义表的结构以获得一些虚拟ID列吗? I do have the ability to do that, but geez! 我有能力做到这一点,但是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.) (我认为我会提到的另一个注意事项是,看起来Zend Framework提供了一种基于复合主键在表上创建类的方法( 参见本页的示例#20 - 大约一半),所以它似乎Magento框架也应该提供它...我只是不知道如何。)

Like most Active Record inspired models, Magento's Mage_Core_Model_Abstract wasn't built with support for composite primary keys in mind. 与大多数Active Record灵感模型一样,Magento的Mage_Core_Model_Abstract并未构建为支持复合主键。 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. 您的选择是转到Magento Model路由并创建一个主键(“假”,就像您所说的那样),然后对表应用唯一索引,或使用基本Zend DB表实现您自己的Model层,或者导入系统中支持所需功能的第三方模型解决方案。

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而言,Magento团队使用Zend的表网关模式为其框架实现Active Record样式模型层 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). Zend Framework不是像Cake或Rails这样的应用程序堆栈,它是一个类库集合,可用于构建应用程序堆栈(或应用程序,或许多其他东西)。 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. 仅仅因为Zend Framework类支持某些东西并不意味着使用Zend Framework的系统和应用程序可以免费获得它。

see that magento Model “Mage_SalesRule_Model_Resource_Coupon_Usage” , the table 'salesrule_coupon_usage' has a compound/composite primary key. 看到magento Model“Mage_SalesRule_Model_Resource_Coupon_Usage”,表'salesrule_coupon_usage'有一个复合/复合主键。 It's like this: 就像这样:

protected function _construct()
{
    $this->_init('salesrule/coupon_usage', '');
}

While you may not be able to use a compound primary key in the standard Active Record style Models, one can easily create a compound primary key in a supporting database migration by setting each field to primary; 虽然您可能无法在标准Active Record样式模型中使用复合主键,但可以通过将每个字段设置为primary来轻松地在支持数据库迁移中创建复合主键。

/**
 * Create table 'cms/block_store'
 */
$table = $installer->getConnection()
    ->newTable($installer->getTable('cms/block_store'))
    ->addColumn('block_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
        'nullable'  => false,
        'primary'   => true,
        ), 'Block ID')
    ->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Store ID')
    ->addIndex($installer->getIdxName('cms/block_store', array('store_id')),
        array('store_id'))
    ->addForeignKey($installer->getFkName('cms/block_store', 'block_id', 'cms/block', 'block_id'),
        'block_id', $installer->getTable('cms/block'), 'block_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
    ->addForeignKey($installer->getFkName('cms/block_store', 'store_id', 'core/store', 'store_id'),
        'store_id', $installer->getTable('core/store'), 'store_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
    ->setComment('CMS Block To Store Linkage Table');
$installer->getConnection()->createTable($table);

I know this is an old question but i had the same issue , but here how i fixed it : i added all the fields separated by comma : 我知道这是一个老问题,但我有同样的问题,但在这里我如何修复它:我添加所有用逗号分隔的字段:

protected function _construct()
{
    $this->_init('salesrule/coupon_usage', 'first_field,second_field,...');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM