1

I have a very big B2B eCommerce which is based on websites (each website has different products, customers etc).

For each website I need to have different databases. I have achieved such a thing but now i need to synchronize some information that will be the same on all websites (so in all databases).

I have a main store that will be the master store and when something changes in CMS Module, Blog Module etc. in master it should be replicated to all other databases.

I do not want to make this replication in sql and i was wondering if it is possible to save/update a model directly by using resource connections of Magento.

0

I did tests and researches and finally came out with a solution. Each store will have its own domain. I have created a master store who knows all other stores including itself. Every other store has just the master store and the current store.

EX US Store has 2 stores

  • US -> us.store.com
  • Master -> master.store.com

In code I created and observer listening to modelnamehere_save_after event for each model i want to sync.

When model is saved and event is triggered, if we are in master store(the store that admin will use) i have a foreach loop as below:

foreach ($websites as $website) {
    $defaultStore = $website->getDefaultStore();
    if (is_null($defaultStore)) {
        continue;
    }
    $baseUrl = $defaultStore->getBaseUrl();
    $serverName = parse_url($baseUrl)['host'] ?? '';
    if (empty($serverName) || ($website->getId() == $mainWebsite->getId()) || ($mainWebsiteServerName == $serverName)) {
        continue;
    }
    $_SERVER['SERVER_NAME'] = $serverName;
    $params = $_SERVER;
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
    $obj = $bootstrap->getObjectManager();

    $modelToReplaceFactory = $obj->get(get_class($object));
    $modelToReplace = $modelToReplaceFactory->load($object->getId());

    $modelToReplace->setData($object->getData());
    $modelToReplace->save();
}

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.