简体   繁体   中英

Magento 1.9.2 create relation between customers and products

I'm trying to create a relationship table between customers and products. The plan is that I will have a separate tab when editing customers to be able to assign products to that customers in a way like 'Related products' tab works when editing products.

So far I was able to add a tab to the admin but the big steps are in front of me. I would like to create a Many-to-many relationship table so I've added a file mysql4-install-0.1.0.php with the below:

$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();

Not sure if the above is correct as at the moment it doesn't even get to this file. I would appreciate a little guidance on how to approach this and also how I can control saving the relationship. I've looked on the internet for similar solution but with no luck.

EDIT 1:
config.xml looks like the below:

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

I've noticed that it doesn't appear in core_resource table. Version in the config is set to 0.1.0 and the installer is named mysql4-install-0.1.0.php and it's placed in the folder: sql > customerproduct_setup

Find a detailed answer below on how to make your Install/Upgrade Script work.

Step 1:

Create your module with the following files:

app/code/local/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

app/code/local/Trike/Customerproduct/sql/customerproduct_setup/install-1.0.0.php

Step 2:

Make sure below CONTENT is present in each of the files:

app/code/local/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 {}

app/code/local/Trike/Customerproduct/sql/customerproduct_setup/install-1.0.0.php

I was able to find an issue with your Install Script code above. So I have updated your code and posting the file (without errors) here:

<?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();

I have tested this in my local machine and it works.

Screenshot as below:

在此处输入图片说明

在此处输入图片说明

Happy Coding...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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