调用Sylius产品变体生成器时为何未生成变体code字段?
问题现象
手动添加1000个产品变体的操作成本极高,因此参考Sylius官方产品开发文档了解动态生成变体的实现方案。官方已提供可基于产品选项生成变体的服务sylius.generator.product_variant,因此编写了如下简单控制台命令进行测试:
<?php namespace App\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Sylius\Component\Product\Model\ProductOptionInterface; use Sylius\Component\Product\Model\ProductOptionValueInterface; use Sylius\Component\Product\Generator\ProductVariantGeneratorInterface; use Sylius\Component\Product\Factory\ProductFactoryInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; class TestCommand extends Command { protected static $defaultName = 'app:test'; /** @var ContainerInterface $_serviceContainer */ private $_serviceContainer; public function __construct(ContainerInterface $fullServiceContainer) { $this->_serviceContainer = $fullServiceContainer; parent::__construct(); } protected function configure() { $this->setDescription('Just testing'); } protected function execute(InputInterface $input, OutputInterface $output) { /** @var ProductFactoryInterface $productFactory **/ $productFactory = $this->_serviceContainer->get('sylius.factory.product'); /** @var ProductInterface $product */ $product = $productFactory->createNew(); $product->setName('T-Shirt'); $product->setCode('00001'); $product->setSlug('t-shirt'); /** @var RepositoryInterface $productRepository */ $productRepository = $this->_serviceContainer->get('sylius.repository.product'); $productRepository->add($product); /** @var ProductOptionInterface $option */ $option = $this->_serviceContainer->get('sylius.factory.product_option')->createNew(); $option->setCode('t_shirt_color'); $option->setName('T-Shirt Color'); // 准备选项值数据 $valuesData = [ 'OV1' => ['locale' => 'en_US', 'value' => 'Red'], 'OV2' => ['locale' => 'en_US', 'value' => 'Blue'], 'OV3' => ['locale' => 'en_US', 'value' => 'Green'], ]; foreach ($valuesData as $code => $values) { /** @var ProductOptionValueInterface $optionValue */ $optionValue = $this->_serviceContainer->get('sylius.factory.product_option_value')->createNew(); $optionValue->setCode($code); $optionValue->setFallbackLocale($values['locale']); $optionValue->setCurrentLocale($values['locale']); $optionValue->setValue($values['value']); $option->addValue($optionValue); } $product->addOption($option); /** @var ProductVariantGeneratorInterface $variantGenerator */ $variantGenerator = $this->_serviceContainer->get('sylius.generator.product_variant'); $variantGenerator->generate($product); // 最终将产品及生成的变体提交到仓库 $productRepository->add($product); return 0; } }
运行命令后抛出数据库约束错误,提示变体表的code字段不能为null:
In AbstractMySQLDriver.php line 107: An exception occurred while executing 'INSERT INTO sylius_product_variant (code, created_at, updated_at, position, enabled, on_hold, on_hand, tracked, width, height, depth, weight, shipping_required, product_id, tax_category_id, shipping_category_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, "2020-11-18 17:33:49", "2020-11-18 17:33:49", 0, 1, 0, 0, 0, null, null, null, null, 1, 13, null, null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'code' cannot be null In PDOStatement.php line 131: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'code' cannot be null In PDOStatement.php line 129: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'code' cannot be null
按常规流程理解,变体的code字段应该是自动生成的,需要确认流程中遗漏了哪些步骤。
问题原因
sylius.generator.product_variant服务的核心职责仅为遍历产品下所有选项的值组合,生成对应的变体实体、关联好对应的选项值,本身不会自动填充code字段- 常规后台操作流程中,变体的唯一编码生成是由独立的
sylius.generator.variant_code服务,配合资源事件监听器在实体持久化前触发的;控制台直接手动调用服务时,这类事件监听器不会自动执行,最终导致生成的变体code字段为空,触发数据库非空约束报错
修复方案
在调用变体生成器完成变体创建后,手动为每个生成的变体生成符合Sylius规则的code,再执行持久化操作。修改后的关键代码片段如下:
// 保持原有变体生成逻辑 $variantGenerator = $this->_serviceContainer->get('sylius.generator.product_variant'); $variantGenerator->generate($product); // 新增:遍历所有生成的变体,为其分配合法code $variantCodeGenerator = $this->_serviceContainer->get('sylius.generator.variant_code'); foreach ($product->getVariants() as $variant) { $variantCode = $variantCodeGenerator->generate($product, $variant); $variant->setCode($variantCode); } // 完成code赋值后再提交仓库持久化 $productRepository->add($product);
注意:原代码中重复调用
$productRepository->add($product)的操作会触发不必要的数据库读写,可以调整为:首次创建产品时不要立刻持久化,等所有选项、变体、code都配置完成后,再统一执行一次仓库add操作,减少不必要的性能消耗。
内容的提问来源于stack exchange,提问作者Vulkhan
相关产品推荐
相关产品推荐

