- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Database Model Event and Observer
Hey, I'm new to Magento programming and hope someone could assist with the below.
I'm busy making a module that will talk to a remote service and I need to save the ID of the remote service to the local database so I can link the local customer with a remote customer. So my question is, is there a easier, cleaner way to do this.
I have this so far, I removed some of the naming etc just for this post.
# app/code/Vendor/Package/Setup/InstallSchema.php
$table = $setup->getConnection()->newTable($setup->getTable('vendor_package_customers'));
$table->addColumn( 'customer_id', Table::TYPE_INTEGER, null, ['unsigned' => true], 'Customer Id');
$table->addColumn( 'remote_id', Table::TYPE_INTEGER, null, ['unsigned' => true], 'Remote Id' );
$table->addIndex($setup->getIdxName('synergicode_sageone_customers', ['customer_id']), ['mage_id']);
$table->addIndex($setup->getIdxName('synergicode_sageone_customers', ['remote_id']), ['sage_id']);
$table->addForeignKey(
$setup->getFkName('synergicode_sageone_customers', 'customer_id', 'customer_entity', 'entity_id'),
'customer_id', $setup->getTable('customer_entity'), 'entity_id',
Table::ACTION_SET_NULL
);
# app/code/Vendor/Package/etc/events.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd"> <event name="adminhtml_customer_save_after"> <observer name="vendor_package_customer_save" instance="Vendor\Package\Observer\Customer\Save" /> </event> </config>
# app/code/Vendor/Package/Observer/Customer.php
class Save implements ObserverInterface { protected $_logger, $_customer_factory; function __construct(\Psr\Log\LoggerInterface $logger, \Vendor\Package\Model\CustomerFactory $customerFactory) { $this->_logger = $logger; $this->_customer_factory = $customerFactory; } public function execute(Observer $observer) { $customer = $observer->getCustomer(); /* Check if the customer object exists TODO */ $_remote_customer = $this->_customer_factory->create()->load($customer->getId()); /* Is there a way to call getByCustomer ? */ $remote_id = $_remote_customer->getRemoteId(); /* Then for inserting a new recording, is there a way to link to the customer object without me having to pass it */ $_db_customer = $this->_customer_factory->create(); $_db_customer->setCustomerId($customer->getId()); $_db_customer->setRemoteId('1234'); $_db_customer->save(); } }
# app/code/Vendor/Package/Model/Customer.php
class Customer extends \Magento\Framework\Model\AbstractModel { /** * Initialize resource model * @return void */ protected function _construct() { $this->_init('Vendor\Package\Model\ResourceModel\Customer'); } /* Should I add methods here so I can access certain stuff in the object ? */ }
# app/code/Vendor/Package/Model/ResourceModel/Customer.php
<?php class Customer extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { /** * Initialize resource model * Get table name from config * * @return void */ protected function _construct() { $this->_init('vendor_package_customers', 'customer_id'); $this->_isPkAutoIncrement = false; } }
# app/local/Vendor/Package/Model/ResourceModel/Customer/Collection.php
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection { /** * Constructor * Configures collection * * @return void */ protected function _construct() { $this->_init('Vendor\Package\Model\Customer', 'Vendor\Package\Model\ResourceModel\Customer'); } }
I hope my question make sense and that someone is willing to assist.