インポート時に製品の製品モデルに変更できる必要があるという要件があります。インポートする CSV ファイルの を変更してこれを実行しようとしましたparent
が、次のメッセージが表示されます。
警告 親: プロパティ「親」は変更できません。「new_parent_code」が指定されています。
これを機能させる適切な方法は何ですか?-テーブルで親を直接編集して製品に別の親を手動で割り当てることにより、データベースを「ハッキング」しようとしましたが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;
}
}
}
}
これは機能します。これで、インポート時に親が適切に保存されます。私は次のことだけを考えています:
- a)。この実装は正しいです。
- b)。親を変更することで、他の大きな問題を引き起こしているわけではありません。
また、親を変更しようとするとエラーをスローするコードの上にある元の Akeneo コードの次の TODO ステートメントにも注目しました。
// TODO: これは PIM-6350 で削除される予定です。
Akeneoの誰かがこれに光を当てることを気にかけていますか?