I’m trying to add an attribute called orchestra_id
to Order
and OrderAddress
. I’ve made a plugin who have been already activated. So I’ve create an Setup\UpgradeData
model. This is what it look like:
<?php namespace Maru3l\SyncOrchestra\Setup; use Magento\Sales\Model\Order; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface { private $ eavSetupFactory; private $ eavConfig; private $ attributeResource; public function __construct( \Magento\Eav\Setup\EavSetupFactory $ eavSetupFactory, \Magento\Eav\Model\Config $ eavConfig, \Magento\Customer\Model\ResourceModel\Attribute $ attributeResource ) { $ this->eavSetupFactory = $ eavSetupFactory; $ this->eavConfig = $ eavConfig; $ this->attributeResource = $ attributeResource; } public function upgrade(ModuleDataSetupInterface $ setup, ModuleContextInterface $ context) { $ setup->startSetup(); $ eavSetup = $ this->eavSetupFactory->create(['setup' => $ setup]); $ this->installAddressOrchestraIdAttribute($ eavSetup); $ this->installOrderOrchestraIdAttribute($ eavSetup); $ setup->endSetup(); } private function installAddressOrchestraIdAttribute($ eavSetup) { $ eavSetup->addAttribute('customer_address', 'orchestra_id', [ 'label' => 'Orchestra ID', 'input' => 'text', 'visible' => true, 'position' => 100, 'unique' => true, 'required' => false, 'system' => false ]); $ attribute = $ this->eavConfig->getAttribute('customer_address', 'orchestra_id'); $ attribute->setData('used_in_forms', ['adminhtml_customer_address', 'customer_address_edit']); $ this->attributeResource->save($ attribute); } private function installOrderOrchestraIdAttribute($ eavSetup) { $ eavSetup->addAttribute(Order::ENTITY, 'orchestra_id', [ 'label' => 'Orchestra ID', 'input' => 'text', 'visible' => true, 'position' => 100, 'unique' => true, 'required' => false, 'system' => false ]); $ attribute = $ this->eavConfig->getAttribute(Order::ENTITY, 'orchestra_id'); $ attribute->setData('used_in_forms', ['adminhtml_order']); $ this->attributeResource->save($ attribute); } }
I’ve literally copy/past the exemple in https://devdocs.magento.com/guides/v2.2/extension-dev-guide/attributes.html but it is not working.
I’m always getting this issue during the php bin/magento setup:upgrade
and I don’t see the collomn appared in the database :
PHP Fatal error: Uncaught Error: Call to a member function getId() on null in /var/www/vhosts/store-api.silverwax.ca/httpdocs/vendor/magento/module-eav/Model/ResourceModel/Attribute.php:117 Stack trace: #0 /var/www/vhosts/store-api.silverwax.ca/httpdocs/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(832): Magento\Eav\Model\ResourceModel\Attribute->_afterSave(Object(Magento\Eav\Model\Entity\Attribute)) #1 /var/www/vhosts/store-api.silverwax.ca/httpdocs/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(419): Magento\Framework\Model\ResourceModel\Db\AbstractDb->processAfterSaves(Object(Magento\Eav\Model\Entity\Attribute)) #2 /var/www/vhosts/store-api.silverwax.ca/httpdocs/generated/code/Magento/Customer/Model/ResourceModel/Attribute/Interceptor.php(258): Magento\Framework\Model\ResourceModel\Db\AbstractDb->save(Object(Magento\Eav\Model\Entity\Attribute)) #3 /var/www/vhosts/store-api.silverwax.ca/httpdocs/app/code/Maru3l/SyncOrchestra/Setup/UpgradeData.php(68): Magento\Customer\Model\ResourceModel\Att in /var/www/vhosts/store-api.silverwax.ca/httpdocs/vendor/magento/module-eav/Model/ResourceModel/Attribute.php on line 117 {"messages":{"error":[{"code":500,"message":"Fatal Error: 'Uncaught Error: Call to a member function getId() on null in \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/vendor\/magento\/module-eav\/Model\/ResourceModel\/Attribute.php:117\nStack trace:\n#0 \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/vendor\/magento\/framework\/Model\/ResourceModel\/Db\/AbstractDb.php(832): Magento\Eav\Model\ResourceModel\Attribute->_afterSave(Object(Magento\Eav\Model\Entity\Attribute))\n#1 \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/vendor\/magento\/framework\/Model\/ResourceModel\/Db\/AbstractDb.php(419): Magento\Framework\Model\ResourceModel\Db\AbstractDb->processAfterSaves(Object(Magento\Eav\Model\Entity\Attribute))\n#2 \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/generated\/code\/Magento\/Customer\/Model\/ResourceModel\/Attribute\/Interceptor.php(258): Magento\Framework\Model\ResourceModel\Db\AbstractDb->save(Object(Magento\Eav\Model\Entity\Attribute))\n#3 \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/app\/code\/Maru3l\/SyncOrchestra\/Setup\/UpgradeData.php(68): Magento\Customer\Model\ResourceModel\Att' in '\/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/vendor\/magento\/module-eav\/Model\/ResourceModel\/Attribute.php' on line 117","trace":"Trace is not available."}]}}
I’ve also create the etc\extension_attributes.xml
:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Maru3l_SyncOrchestra" setup_version="0.0.18"> <sequence> <module name="Magento_Cusomer"/> <module name="Magento_InventoryApi"/> <module name="Magento_Sales"/> </sequence> </module> </config>