Magento – Where the actual data of custom attribute is stored in magento 2

databaseeavmagento2

I have added a custom attribute in existing table catalog_category. The following code i have write in InstallData.php.

namespace BelVG\AdditionalImageTemplate\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;

/**
 * @codeCoverageIgnore
 */
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
    ) {
        $installer = $setup;
        $setup->startSetup();
        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
        $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);        
        $categorySetup->addAttribute(
                'catalog_category', 'additional_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',
                ]
        );        
        $installer->endSetup();
        $setup->endSetup();
    }

}

This works fine and I got the entry in eav_attribute table.
After that I created a Thumbnail Image in backend category form and that too works fine.

But I did not get where the actual values of the image I upload from backend side is stored and how to fetch that values?

Basically I need the image name and url of the image.

Best Answer

The question is simple.

1,> Get Custom category attribute the attribute_id. To the eav_attribute table query attribute_code = custom.attribute.name.

2,> Query catalog_category_entity_varchar using attribute_id.

So, Custom attribute is stored in Database.catalog_category_entity_varchar.

Calling method:

// \Magento\Catalog\Model\CategoryFactory $categoryFactory,
$this->categoryFactory = $categoryFactory;
$currentCat = $this->categoryFactory->create()->load($modelId);
$catImg = $currentCat->getData('Custom category attribute name');
Related Topic