php - Magento 1.9.2 create relation between customers and products -
i'm trying create relationship table between customers , products. plan have separate tab when editing customers able assign products customers in way 'related products' tab works when editing products.
so far able add tab admin big steps in front of me. create many-to-many relationship table i've added file mysql4-install-0.1.0.php 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 above correct @ moment doesn't file. appreciate little guidance on how approach , how can control saving relationship. i've looked on internet similar solution no luck.
edit 1:
config.xml looks 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 doesn't appear in core_resource table. version in config set 0.1.0 , installer named mysql4-install-0.1.0.php , it's placed in folder: sql > customerproduct_setup
find detailed answer below on how make install/upgrade script work.
step 1:
create module 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 present in each of 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 able find issue install script code above. have updated code , posting 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 in local machine , works.
screenshot below:
happy coding...
Comments
Post a Comment