[英]Magento 1.9.2 create relation between customers and products
我正在尝试创建客户与产品之间的关系表。 计划是,在编辑客户时,我将有一个单独的选项卡,以便能够像在编辑产品时使用“相关产品”选项卡的方式将产品分配给该客户一样。
到目前为止,我已经能够向管理员添加标签,但是重要的步骤就在我眼前。 我想创建一个多对多关系表,所以我添加了一个带有以下内容的文件mysql4-install-0.1.0.php:
$installer = $this;
$installer->startSetup();
/**
* Create table 'customerproduct/product_relation'
*/
$table = $installer->getConnection()
->newTable($installer->getTable('customerproduct/product_relation'))
->addColumn('customer_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'unsigned' => true,
'nullable' => false,
'primary' => true,
), 'Customer ID')
->addColumn('product_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'unsigned' => true,
'nullable' => false,
'primary' => true,
), 'Product ID')
->addIndex($installer->getIdxName('customerproduct/product_relation', array('product_id')),
array('product_id'))
->addForeignKey($installer->getFkName('customerproduct/product_relation', 'product_id', 'customer/product', 'entity_id'),
'product_id', $installer->getTable('catalog/product'), 'entity_id',
Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
->addForeignKey($installer->getFkName('customerproduct/product_relation', 'customer_id', 'customer/entity', 'entity_id'),
'customer_id', $installer->getTable('customer/entity'), 'entity_id',
Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
->setComment('Customer Product Relation Table');
$installer->getConnection()->createTable($table);
$installer->endSetup();
$installer->installEntities();
不确定以上内容是否正确,因为目前还没有找到该文件。 我将对如何处理此问题以及如何控制保存关系提供一些指导。 我一直在互联网上寻找类似的解决方案,但是没有运气。
编辑1:
config.xml如下所示:
<config>
<modules>
<Trike_Customerproduct>
<version>0.1.0</version>
</Trike_Customerproduct>
</modules>
<adminhtml>
<layout>
<updates>
<customerproduct>
<file>trike_customerproduct.xml</file>
</customerproduct>
</updates>
</layout>
</adminhtml>
<global>
<blocks>
<customerproduct>
<class>Trike_Customerproduct_Block</class>
</customerproduct>
</blocks>
</global>
</config>
我注意到它没有出现在core_resource表中。 配置中的版本设置为0.1.0,安装程序名为mysql4-install-0.1.0.php,并将其放置在以下文件夹中:sql> customerproduct_setup
在下面找到有关如何使安装/升级脚本正常工作的详细答案。
第1步:
使用以下文件创建模块:
应用程序/代码/本地/Trike/Customerproduct/etc/config.xml
app / code / local / Trike / Customerproduct / Model / Customerproduct.php
app / code / local / Trike / Customerproduct / Model / Resource / Customerproduct.php
app / code / local / Trike / Customerproduct / Model / Resource / Customerproduct / Collection.php
app / code / local / Trike / Customerproduct / Model / Resource / Setup.php
应用程序/代码/本地/Trike/Customerproduct/sql/customerproduct_setup/install-1.0.0.php
第2步:
确保每个文件中都有以下CONTENT:
应用程序/代码/本地/Trike/Customerproduct/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Trike_Customerproduct>
<version>1.0.0</version>
</Trike_Customerproduct>
</modules>
<global>
<blocks>
<customerproduct>
<class>Trike_Customerproduct_Block</class>
</customerproduct>
</blocks>
<models>
<customerproduct>
<class>Trike_Customerproduct_Model</class>
<resourceModel>customerproduct_resource</resourceModel>
</customerproduct>
<customerproduct_resource>
<class>Trike_Customerproduct_Model_Resource</class>
<entities>
<product_relation>
<table>customer_product_relation</table>
</product_relation>
</entities>
</customerproduct_resource>
</models>
<resources>
<customerproduct_setup>
<setup>
<module>Trike_Customerproduct</module>
<class>Trike_Customerproduct_Model_Resource_Setup</class>
</setup>
</customerproduct_setup>
</resources>
</global>
<adminhtml>
<layout>
<updates>
<customerproduct>
<file>trike_customerproduct.xml</file>
</customerproduct>
</updates>
</layout>
</adminhtml>
</config>
app / code / local / Trike / Customerproduct / Model / Customerproduct.php
<?php
class Trike_Customerproduct_Model_Customerproduct extends Mage_Catalog_Model_Abstract
{
protected function _construct()
{
$this->_init('customerproduct/product_relation');
}
}
app / code / local / Trike / Customerproduct / Model / Resource / Customerproduct.php
<?php
class Trike_Customerproduct_Model_Resource_Customerproduct extends Mage_Catalog_Model_Resource_Abstract
{
protected function _construct()
{
$this->_init('customerproduct/product_relation','customer_id');
}
}
app / code / local / Trike / Customerproduct / Model / Resource / Customerproduct / Collection.php
<?php
class Trike_Customerproduct_Model_Resource_Customerproduct_Collection extends Mage_Catalog_Model_Resource_Product_Collection
{
protected function _construct()
{
$this->_init('customerproduct/product_relation');
}
}
app / code / local / Trike / Customerproduct / Model / Resource / Setup.php
<?php
class Trike_Customerproduct_Model_Resource_Setup extends Mage_Catalog_Model_Resource_Setup {}
应用程序/代码/本地/Trike/Customerproduct/sql/customerproduct_setup/install-1.0.0.php
我在上面的安装脚本代码中发现了一个问题。 因此,我已经更新了您的代码,并在此处发布了文件(无错误):
<?php
$installer = $this;
$installer->startSetup();
/**
* Create table 'customerproduct/product_relation'
*/
$table = $installer->getConnection()
->newTable($installer->getTable('customerproduct/product_relation'))
->addColumn('customer_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'unsigned' => true,
'nullable' => false,
'primary' => true,
), 'Customer ID')
->addColumn('product_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'unsigned' => true,
'nullable' => false,
'primary' => true,
), 'Product ID')
->addIndex($installer->getIdxName('customerproduct/product_relation', array('product_id')),
array('product_id'))
->addForeignKey($installer->getFkName('customerproduct/product_relation', 'product_id', 'catalog/product', 'entity_id'),
'product_id', $installer->getTable('catalog/product'), 'entity_id',
Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
->addForeignKey($installer->getFkName('customerproduct/product_relation', 'customer_id', 'customer/entity', 'entity_id'),
'customer_id', $installer->getTable('customer/entity'), 'entity_id',
Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
->setComment('Customer Product Relation Table');
$installer->getConnection()->createTable($table);
$installer->endSetup();
$installer->installEntities();
我已经在本地计算机上对此进行了测试,并且可以正常工作。
屏幕截图如下:
快乐编码...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.