1

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.

1
  • 1
    If its category attribute and type is varchar then catalog_category_entity_varchar should be the table. There you need to join eav_attribute and catalog_category_entity_varchar to get your desired value Commented Apr 3, 2017 at 13:21

1 Answer 1

0

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.