Skip to content

Fix multiselect with a lot of options not saved to DB #33487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function _getIndexableAttributes($multiSelect)
);

if ($multiSelect == true) {
$select->where('ea.backend_type = ?', 'varchar')->where('ea.frontend_input = ?', 'multiselect');
$select->where('ea.backend_type = ?', 'text')->where('ea.frontend_input = ?', 'multiselect');
} else {
$select->where('ea.backend_type = ?', 'int')->where('ea.frontend_input IN( ? )', ['select', 'boolean']);
}
Expand Down Expand Up @@ -303,14 +303,14 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
// prepare get multiselect values query
$productValueExpression = $connection->getCheckSql('pvs.value_id > 0', 'pvs.value', 'pvd.value');
$select = $connection->select()->from(
['pvd' => $this->getTable('catalog_product_entity_varchar')],
['pvd' => $this->getTable('catalog_product_entity_text')],
[]
)->join(
['cs' => $this->getTable('store')],
'',
[]
)->joinLeft(
['pvs' => $this->getTable('catalog_product_entity_varchar')],
['pvs' => $this->getTable('catalog_product_entity_text')],
"pvs.{$productIdField} = pvd.{$productIdField} AND pvs.attribute_id = pvd.attribute_id"
. ' AND pvs.store_id=cs.store_id',
[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Setup\Patch\Data;

use Magento\Catalog\Model\Product;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class UpdateMultiselectAttributesBackendTypes implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $dataSetup;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;

/**
* MigrateMultiselectAttributesData constructor.
* @param ModuleDataSetupInterface $dataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $dataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->dataSetup = $dataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}

/**
* @inheritdoc
*/
public static function getDependencies()
{
return [];
}

/**
* @inheritdoc
*/
public function getAliases()
{
return [];
}

/**
* @inheritdoc
*/
public function apply()
{
$this->dataSetup->startSetup();

$connection = $this->dataSetup->getConnection();
$attributeTable = $connection->getTableName('eav_attribute');
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->dataSetup]);
$entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY);
$attributesToMigrate = $connection->fetchCol(
$connection
->select()
->from($attributeTable, ['attribute_id'])
->where('entity_type_id = ?', $entityTypeId)
->where('backend_type = ?', 'varchar')
->where('frontend_input = ?', 'multiselect')
);

$varcharTable = $connection->getTableName('catalog_product_entity_varchar');
$textTable = $connection->getTableName('catalog_product_entity_text');
$varcharTableDataSql = $connection
->select()
->from($varcharTable)
->where('attribute_id in (?)', $attributesToMigrate);
$dataToMigrate = array_map(static function ($row) {
$row['value_id'] = null;
return $row;
}, $connection->fetchAll($varcharTableDataSql));

foreach (array_chunk($dataToMigrate, 2000) as $dataChunk) {
$connection->insertMultiple($textTable, $dataChunk);
}

$connection->query($connection->deleteFromSelect($varcharTableDataSql, $varcharTable));

foreach ($attributesToMigrate as $attributeId) {
$eavSetup->updateAttribute($entityTypeId, $attributeId, 'backend_type', 'text');
}

$this->dataSetup->endSetup();

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected function addGlobalAttribute(
$linkField = $attribute->getEntity()->getLinkField();

$collection->getSelect()->join(
[$alias => $collection->getTable('catalog_product_entity_varchar')],
[$alias => $collection->getTable($attribute->getBackendTable())],
"($alias.$linkField = e.$linkField) AND ($alias.store_id = $storeId)" .
" AND ($alias.attribute_id = {$attribute->getId()})",
[]
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Eav/Model/Entity/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,12 @@ public function getBackendTypeByInput($type)
case 'text':
case 'gallery':
case 'media_image':
case 'multiselect':
$field = 'varchar';
break;

case 'image':
case 'textarea':
case 'multiselect':
$field = 'text';
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ private function clearSelectedOptionInEntities(AbstractModel $object, int $optio
$where = $connection->quoteInto('attribute_id = ?', $attributeId);
$update = [];

if ($object->getBackendType() === 'varchar') {
if ($object->getBackendType() === 'text') {
$where.= ' AND ' . $connection->prepareSqlCondition('value', ['finset' => $optionId]);
$concat = $connection->getConcatSql(["','", 'value', "','"]);
$expr = $connection->quoteInto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function dataGetBackendTypeByInput()
['text', 'varchar'],
['gallery', 'varchar'],
['media_image', 'varchar'],
['multiselect', 'varchar'],
['multiselect', 'text'],
['image', 'text'],
['textarea', 'text'],
['date', 'datetime'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function getUpdateExpectedData(): array
'frontend_class' => null,
'used_for_sort_by' => '0',
'is_user_defined' => '1',
'backend_type' => 'varchar',
'backend_type' => 'text',
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,25 @@ public function testGetAttributeTextArray()
);
}

/**
* @magentoDataFixture Magento/Catalog/_files/products_with_multiselect_attribute.php
*/
public function testMultipleMultiselectTextValues()
{
$expectedArray = [];

for ($i = 1; $i < 200; $i++) {
$expectedArray[] = sprintf('Multiselect option %d', $i);
}

$product = $this->productRepository->get('simple_ms_3');

self::assertEquals(
$expectedArray,
$product->getAttributeText('multiselect_attribute_text')
);
}

public function testGetCustomDesignDate()
{
$this->assertEquals(['from' => null, 'to' => null], $this->_model->getCustomDesignDate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Magento\Store\Api\Data\StoreInterface;

/**
* Class SourceTest
* @magentoAppIsolation enabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
Expand Down Expand Up @@ -69,7 +68,7 @@ public function testReindexEntitiesForConfigurableProduct()

/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attr **/
$attr = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class)
->getAttribute('catalog_product', 'test_configurable');
->getAttribute('catalog_product', 'test_configurable');
$attr->setIsFilterable(1)->save();

$this->_eavIndexerProcessor->reindexAll();
Expand Down Expand Up @@ -133,7 +132,7 @@ public function testReindexMultiselectAttribute()

/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attr **/
$attr = $objectManager->get(\Magento\Eav\Model\Config::class)
->getAttribute('catalog_product', 'multiselect_attribute');
->getAttribute('catalog_product', 'multiselect_attribute');

/** @var $options \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */
$options = $objectManager->create(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class);
Expand Down Expand Up @@ -213,7 +212,7 @@ public function testReindexMultiselectAttributeWithSourceModel()

/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attr **/
$attr = $objectManager->get(\Magento\Eav\Model\Config::class)
->getAttribute('catalog_product', 'multiselect_attr_with_source');
->getAttribute('catalog_product', 'multiselect_attr_with_source');

/** @var $sourceModel MultiselectSourceMock */
$sourceModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
Expand All @@ -231,8 +230,8 @@ public function testReindexMultiselectAttributeWithSourceModel()

/** @var \Magento\Catalog\Model\Product $product2 **/
$product2 = $productRepository->getById($product2Id);
$product1->setSpecialFromDate(date('Y-m-d H:i:s'));
$product1->setNewsFromDate(date('Y-m-d H:i:s'));
$product2->setSpecialFromDate(date('Y-m-d H:i:s'));
$product2->setNewsFromDate(date('Y-m-d H:i:s'));
$productRepository->save($product2);

$this->_eavIndexerProcessor->reindexAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@
$installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Setup\CategorySetup::class
);
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
/** @var $attributeMultiselect \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$attributeMultiselect = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
);

/** @var $attributeMultiselectText \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$attributeMultiselectText = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
);

$valueOptionArray = [];
$orderArray = [];

for ($i = 1; $i < 200; $i++) {
$valueOptionArray[sprintf('option_%d', $i)] = [sprintf('Multiselect option %d', $i)];
$orderArray[sprintf('option_%d', $i)] = $i;
}

$entityType = $installer->getEntityTypeId('catalog_product');
if (!$attribute->loadByCode($entityType, 'multiselect_attribute')->getAttributeId()) {
$attribute->setData(
if (!$attributeMultiselect->loadByCode($entityType, 'multiselect_attribute')->getAttributeId()) {
$attributeMultiselect->setData(
[
'attribute_code' => 'multiselect_attribute',
'entity_type_id' => $entityType,
Expand All @@ -34,7 +48,7 @@
'used_in_product_listing' => 0,
'used_for_sort_by' => 0,
'frontend_label' => ['Multiselect Attribute'],
'backend_type' => 'varchar',
'backend_type' => 'text',
'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
'option' => [
'value' => [
Expand All @@ -52,8 +66,45 @@
],
]
);
$attribute->save();
$attributeMultiselect->save();

/* Assign attribute to attribute set */
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attributeMultiselect->getId());
}


if (!$attributeMultiselectText->loadByCode($entityType, 'multiselect_attribute_text')->getAttributeId()) {
$attributeMultiselectText->setData(
[
'attribute_code' => 'multiselect_attribute_text',
'entity_type_id' => $entityType,
'is_global' => 1,

'is_user_defined' => 1,
'frontend_input' => 'multiselect',
'is_unique' => 0,
'is_required' => 0,
'is_searchable' => 0,
'is_visible_in_advanced_search' => 0,
'is_comparable' => 0,
'is_filterable' => 1,
'is_filterable_in_search' => 0,
'is_used_for_promo_rules' => 0,
'is_html_allowed_on_front' => 1,
'is_visible_on_front' => 0,
'used_in_product_listing' => 0,
'used_for_sort_by' => 0,
'frontend_label' => ['Multiselect Attribute'],
'backend_type' => 'text',
'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
'option' => [
'value' => $valueOptionArray,
'order' => $orderArray
],
]
);
$attributeMultiselectText->save();

/* Assign attribute to attribute set */
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attributeMultiselectText->getId());
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
$attribute->load('multiselect_attribute', 'attribute_code');
$attribute->delete();

$attribute->load('multiselect_attribute_text', 'attribute_code');
$attribute->delete();

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
'used_in_product_listing' => 0,
'used_for_sort_by' => 0,
'frontend_label' => ['Multiselect Attribute'],
'backend_type' => 'varchar',
'backend_type' => 'text',
'backend_model' => ArrayBackend::class,
'option' => [
'value' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'used_in_product_listing' => 0,
'used_for_sort_by' => 0,
'frontend_label' => ['Multiselect Attribute'],
'backend_type' => 'varchar',
'backend_type' => 'text',
'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
'option' => [
'value' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'used_in_product_listing' => 0,
'used_for_sort_by' => 0,
'frontend_label' => ['Multiselect Attribute with Source Model'],
'backend_type' => 'varchar',
'backend_type' => 'text',
'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
'source_model' => \Magento\Catalog\_files\MultiselectSourceMock::class
]
Expand Down
Loading