Skip to content

Instantly share code, notes, and snippets.

@programgames
Created November 26, 2021 13:21
Show Gist options
  • Save programgames/b82c870b683fdff24b8d0cc10d3c31b7 to your computer and use it in GitHub Desktop.
Save programgames/b82c870b683fdff24b8d0cc10d3c31b7 to your computer and use it in GitHub Desktop.
Product Attribute migration
Product attributes :
<?php
declare(strict_types=1);
namespace namespace;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Oro\Bundle\EntityConfigBundle\Config\ConfigManager;
use Oro\Bundle\EntityConfigBundle\Entity\FieldConfigModel;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\ProductBundle\Entity\Product;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class UpdateProductAttributeIndexes extends AbstractFixture implements ContainerAwareInterface
{
use ContainerAwareTrait;
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$this->updateProductAttributes(
[
'attribute1' => [
'is_attribute' => true,
'is_system' => false,
'visible'=> true,
'enabled' => true,
'filterable' => true,
],
'attribute2' => [
'is_attribute' => true,
'is_system' => true,
'visible'=> true,
'enabled' => true,
'filterable' => true,
],
'attribute3' => [
'is_attribute' => true,
'is_system' => false,
'visible'=> true,
'enabled' => true,
'filterable' => true,
],
],
'attribute'
);
$this->updateProductAttributes(
[
'grade' => [
'owner' => ExtendScope::OWNER_SYSTEM,
],
'oroCatalog' => [
'owner' => ExtendScope::OWNER_SYSTEM,
],
],
'extend'
);
$this->getConfigManager()->flush();
}
private function updateProductAttributes(array $fields, $scope)
{
$configManager = $this->getConfigManager();
$entityManager = $configManager->getEntityManager();
foreach ($fields as $field => $attributeOptions) {
$fieldConfigModel = $configManager->getConfigFieldModel(Product::class, $field);
$this->updateFieldConfigs($fieldConfigModel, [$scope => $attributeOptions]);
$entityManager->persist($fieldConfigModel);
}
$entityManager->flush();
}
/**
* @return ConfigManager
*/
private function getConfigManager()
{
return $this->container->get('oro_entity_config.config_manager');
}
public function updateFieldConfigs(FieldConfigModel $fieldModel, $options)
{
$className = $fieldModel->getEntity()->getClassName();
$fieldName = $fieldModel->getFieldName();
foreach ($options as $scope => $scopeValues) {
$configProvider = $this->getConfigManager()->getProvider($scope);
$config = $configProvider->getConfig($className, $fieldName);
$hasChanges = false;
foreach ($scopeValues as $code => $val) {
$config->set($code, $val);
$hasChanges = true;
}
if ($hasChanges) {
$this->getConfigManager()->persist($config);
$indexedValues = $configProvider->getPropertyConfig()->getIndexedValues($config->getId());
$fieldModel->fromArray($config->getId()->getScope(), $config->all(), $indexedValues);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment