eav_attributes not adding to database with setup:upgrade
-
11-12-2020 - |
Question
I am trying to add some custom attributes to my Category page, without success. I created a custom module with its own setup script, after running:
php bin/magento setup:upgrade
There are no attributes added to my databsase. Here are are my module files:
app/code/Forza/CategoriesBanner/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Forza_CategoriesBanner',
__DIR__
);
app/code/Forza/CategoriesBanner/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Forza_CategoriesBanner" setup_version="1.0.0">
</module>
</config>
app/code/Forza/CategoriesBanner/Setup/InstallData.php
<?php
namespace Forza\CategoriesBanner\Setup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
'banner_image',
[
'type' => 'varchar',
'label' => 'Banner Image',
'input' => 'image',
'required' => false,
'sort_order' => 6,
'backend' => 'Forza\CategoriesBanner\Model\Category\Attribute\Backend\CategoryBannerImage',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
]
);
$setup->endSetup();
}
}
?>
I am not getting errors on setup:upgrade, so don't really know where to look. What could be the cause of this?
Solution
if you want to add Image attribute then try below code in InstallData.php
<?php
namespace Namespace\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;
class InstallData
implements InstallDataInterface
{
/**
* Category setup factory
*
* @var CategorySetupFactory
*/
private $categorySetupFactory;
/**
* Init
*
* @param CategorySetupFactory $categorySetupFactory
*/
public function __construct(
CategorySetupFactory $categorySetupFactory
) {
$this->categorySetupFactory = $categorySetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
$categorySetup->addAttribute(
'catalog_category',
'banner_image',
[
'type' => 'varchar',
'label' => 'Additional Image',
'input' => 'image',
'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
'required' => false,
'sort_order' => 100,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
]
);
$setup->endSetup();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange