cancel
Showing results for 
Search instead for 
Did you mean: 

What's the alternative to save() method?

What's the alternative to save() method?

Hello, I'm trying to create store and store view programmatically, but I'm having trouble inserting the store into the database.

 

public function createStores()
{
// create english group
$englishGroup = $this->groupFactory->create();
$englishGroup->setName("englishGroup");
$englishGroup->setCode("Task5EnglishGroup");
$englishGroup->setWebsiteId(self::DEFAULT_WEB_ID);
$englishGroup->setRootCategoryId(self::DEFAULT_ROOT_CATEGORY);

// create german group
$germanGroup = $this->groupFactory->create();
$germanGroup->setName("germanGroup");
$germanGroup->setCode("Task5GermanGroup");
$germanGroup->setWebsiteId(self::DEFAULT_WEB_ID);
$germanGroup->setRootCategoryId(self::DEFAULT_ROOT_CATEGORY);

// create german store view
$germanStore = $this->storeFactory->create();
$germanStore->setName("germanStoreView");
$germanStore->setCode("Task5GermanView");
$germanStore->setGroup($germanGroup);
$germanStore->setIsActive(1);

// create english store view
$englishStore = $this->storeFactory->create();
$englishStore->setName("englishStoreView");
$englishStore->setCode("Task5EnglishStoreView");
$englishStore->setGroup($englishGroup);
$englishStore->setIsActive(1);
$englishStore->setCurrentCurrencyCode("GBP");
}

 $englishGroup->save() could have solved my problem, but it is deprecated. What do I need to use to successfully create store and store view? Any kind of help would be appreciated

2 REPLIES 2

Re: What's the alternative to save() method?

Hello @tomamargisf3b6 

 

use ResourceModel save method For Save
$englishStore = $this->storeFactory->create();
    $englishStore->setName("englishStoreView");
    $englishStore->setCode("Task5EnglishStoreView");
    $englishStore->setGroup($englishGroup);
    $englishStore->setIsActive(1);
    $englishStore->setCurrentCurrencyCode("GBP");


$englishStoreResourceModel = $this->storeResourceModelFactory->create();
$englishStoreResourceModel->save($englishStore)
I hope it helps.
Problem solved? Click Kudos and "Accept as Solution"

Re: What's the alternative to save() method?

call save function on resourcemodel like this:

 

public function __construct(

\vendor\modulename\model\resourcemodel\file $resource

) {

$this->englistGroupResouce = $resource;

}

.....code...


$this->englishGroupResource->save($englishGroup);