如果我想通过UpgradeData.php在magento2中添加一个新的Category,我怎么能做到呢?只需要在catalog_Category_entity表中添加一条新记录?但是我在这个表中找不到类别名称。
将MagentoCatalogModelCategoryFactory
和MagentoCatalogModelCategoryRepository
注入UpgradeData类:
public function __construct(
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoCatalogModelCategoryRepository $categoryRepository
) {
$this->categoryFactory = $categoryFactory;
$this->categoryRepository = $categoryRepository;
}
创建一个新的类别实例并设置所需的数据:
$category = $this->categoryFactory->create();
$category->setData('name', 'CATEGORY_NAME');
[...] // set other category data here
保存新类别:
$this->categoryRepository->save($category)