1

我有要求在导入时我需要能够更改为产品的产品型号。我尝试通过更改parent我正在导入的 CSV 文件中的 来做到这一点,但这将显示以下消息:

警告父:属性“父”不能被修改,“new_parent_code”给定。

使这项工作的正确方法是什么?我尝试通过直接在 -table 中编辑父级手动为产品分配不同的父级来“破解”数据库pim_catalog_product,这似乎可行,但是在编辑产品时会出现意外结果。

谁能指出我在导入时如何更改产品父级的正确方向?

更新:

我现在想出了以下解决方案:

在我自己的包中,我添加了Resources/config/updaters.yml(使用 DependencyInjecten 扩展)以下内容:

parameters:
    # Rewrite parent field setter so we can allow the importer to update the parent:
    pim_catalog.updater.setter.parent_field.class:            Vendor\Bundle\InstallerBundle\Updater\Setter\ParentFieldSetter

我的习惯ParentFieldSetter.php

namespace Vendor\Bundle\InstallerBundle\Updater\Setter;

use Akeneo\Component\StorageUtils\Exception\ImmutablePropertyException;
use Akeneo\Component\StorageUtils\Repository\IdentifiableObjectRepositoryInterface;

/**
 * Class ParentFieldSetter
 */
class ParentFieldSetter extends \Pim\Component\Catalog\Updater\Setter\ParentFieldSetter
{
    /**
     * @var IdentifiableObjectRepositoryInterface
     */
    private $productModelRepository;

    /**
     * ParentFieldSetter constructor.
     * @param IdentifiableObjectRepositoryInterface $productModelRepository
     * @param array $supportedFields
     */
    public function __construct(
        IdentifiableObjectRepositoryInterface $productModelRepository,
        array $supportedFields
    ) {
        $this->productModelRepository = $productModelRepository;
        parent::__construct($productModelRepository, $supportedFields);
    }

    /**
     * @param \Pim\Component\Catalog\Model\ProductInterface|\Pim\Component\Catalog\Model\ProductModelInterface $product
     * @param string $field
     * @param mixed $data
     * @param array $options
     */
    public function setFieldData($product, $field, $data, array $options = []): void
    {
        try {
            parent::setFieldData($product, $field, $data, $options);
        } catch (ImmutablePropertyException $exception) {
            if ($exception->getPropertyName() === 'parent') {
                // Allow us to change the product parent:
                if ($parent = $this->productModelRepository->findOneByIdentifier($data)) {
                    $familyVariant = $parent->getFamilyVariant();
                    $product->setParent($parent);
                    $product->setFamilyVariant($familyVariant);
                    if (null === $product->getFamily()) {
                        $product->setFamily($familyVariant->getFamily());
                    }
                }
            } else {
                throw $exception;
            }
        }
    }
}

这行得通。现在,在导入父级时,会正确保存。我只是想知道是否:

  • 一个)。这个实现是正确的。
  • 乙)。我不会通过更改父母来引起其他一些重大问题。

我还在尝试更改父级时引发错误的代码上方的原始 Akeneo 代码中注意到以下 TODO 语句:

// TODO: 这将在 PIM-6350 中删除。

Akeneo 的任何人都愿意对此有所了解吗?

4

0 回答 0