2016-02-05 54 views
0

我正在尝试创建客户和产品之间的关系表。我们的计划是,在编辑客户时,我将拥有一个单独的选项卡,以便在编辑产品时以“相关产品”选项卡的方式将产品分配给该客户。Magento 1.9.2创建客户和产品之间的关系

到目前为止,我能够向管理员添加一个选项卡,但大步骤在我面前。我想创建一个多一对多的关系表,所以我添加了一个文件mysql4安装-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

第一个要解决的问题是,为什么没有被访问的安装文件。它在模块的config.xml中如何链接? – Reflexorozy

+0

尝试从core_resources表中删除此模块的相应行。在创建安装文件之前,您的模块可能已经注册了0.1.0版本。如果它不起作用,请粘贴此模块的config.xml。 –

回答

0

查找以下详细的答案,了解如何使您的安装/升级脚本工作。

第1步:

与下列文件创建你的模块:

应用程序/代码/本地/三轮车/ Customerproduct的/ etc/config.xml中

应用程序/代码/ local/Trike/Customerproduct/Model/Customerproduct.php

app/code/local/Trike/Customerproduct/Model/Resource/Customerproduct.php

应用程序/代码/本地/三轮车/ Customerproduct /型号/资源/ Customerproduct/Collection.php

应用程序/代码/本地/三轮车/ Customerproduct /型号/资源/ Setup.php

应用程序/代码/local/Trike/Customerproduct/sql/customerproduct_setup/install-1.0.0.php

第2步:

确保以下内容存在于每个文件:

应用程序/代码/本地/三轮车/ 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> 

应用程序/代码/本地/三轮车/ Customerproduct /型号/ Customerproduct。PHP

<?php 
class Trike_Customerproduct_Model_Customerproduct extends Mage_Catalog_Model_Abstract 
{ 
    protected function _construct() 
    { 
     $this->_init('customerproduct/product_relation'); 
    } 
} 

应用程序/代码/本地/三轮车/ Customerproduct /型号/资源/ 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'); 
    } 
} 

应用程序/代码/本地/三轮车/ Customerproduct /型号/资源/ 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'); 
    } 
} 

应用程序/代码/本地/三轮车/ Customerproduct /型号/资源/ Setup.php

<?php 
class Trike_Customerproduct_Model_Resource_Setup extends Mage_Catalog_Model_Resource_Setup {} 

应用程序/代码/本地/三轮车/ Customerproduct/SQL customerproduct_setup /安装-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(); 

我在本地机器上测试,这和它的作品。

截图如下:

enter image description here

enter image description here

编码快乐......

+0

马上工作,谢谢你Shivani。 现在对我来说,下一步是想出如何在编辑客户详细信息时管理这些工具。我试图从Mage_Adminhtml_Block_Catalog_Category_Tab_Product中得到这个想法,但是很少有位已经混淆了,因为我正在努力为此找到一个模板。 –