Magento 2 : UpgradeData.php not working
I need to add a custom attribute (contact_number) for customer in Magento2 via an existing module, so I am using UpgradeData.php. Can anyone please tell what I have missed out.
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoCustomerModelCustomer;
class UpgradeData implements UpgradeDataInterface {
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
//if (version_compare($context->getVersion(), "1.0.1", "<"))
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'contact_number', array(
[
'type' => 'varchar',
'label' => 'Contact Number',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
));
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'contact_number');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'
]
);
$Attribute->save();
$setup->endSetup();
}
}
}
Not getting what I am doing wrong. Any help, please.
[edit]
: Not getting the label in the backend as well:
Please find the screen-shot attached.
magento2 magento2.2 upgrade-script upgradeschema
add a comment |
I need to add a custom attribute (contact_number) for customer in Magento2 via an existing module, so I am using UpgradeData.php. Can anyone please tell what I have missed out.
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoCustomerModelCustomer;
class UpgradeData implements UpgradeDataInterface {
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
//if (version_compare($context->getVersion(), "1.0.1", "<"))
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'contact_number', array(
[
'type' => 'varchar',
'label' => 'Contact Number',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
));
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'contact_number');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'
]
);
$Attribute->save();
$setup->endSetup();
}
}
}
Not getting what I am doing wrong. Any help, please.
[edit]
: Not getting the label in the backend as well:
Please find the screen-shot attached.
magento2 magento2.2 upgrade-script upgradeschema
have You use 'php bin/magento setup:upgrade' ?
– Konrad Siamro
Jun 21 '18 at 8:50
yes i did, but i think that their is some another issue as when in upgrade function for debug i did echo 'hi'; exit;. Their came no error in setup:upgrade
– Narendra
Jun 21 '18 at 8:58
I am a bit confused that what i should use installdata or upgradedata
– Narendra
Jun 21 '18 at 8:59
I got the point. Actually we use upgradedata when module is already installed but module version should also change but i was not changing the module version so the changes were not reflecting. Thanks by the way
– Narendra
Jun 21 '18 at 9:03
Please check my updated answer.
– Rohan Hapani
Jun 21 '18 at 9:31
add a comment |
I need to add a custom attribute (contact_number) for customer in Magento2 via an existing module, so I am using UpgradeData.php. Can anyone please tell what I have missed out.
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoCustomerModelCustomer;
class UpgradeData implements UpgradeDataInterface {
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
//if (version_compare($context->getVersion(), "1.0.1", "<"))
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'contact_number', array(
[
'type' => 'varchar',
'label' => 'Contact Number',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
));
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'contact_number');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'
]
);
$Attribute->save();
$setup->endSetup();
}
}
}
Not getting what I am doing wrong. Any help, please.
[edit]
: Not getting the label in the backend as well:
Please find the screen-shot attached.
magento2 magento2.2 upgrade-script upgradeschema
I need to add a custom attribute (contact_number) for customer in Magento2 via an existing module, so I am using UpgradeData.php. Can anyone please tell what I have missed out.
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoCustomerModelCustomer;
class UpgradeData implements UpgradeDataInterface {
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
//if (version_compare($context->getVersion(), "1.0.1", "<"))
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'contact_number', array(
[
'type' => 'varchar',
'label' => 'Contact Number',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
));
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'contact_number');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'
]
);
$Attribute->save();
$setup->endSetup();
}
}
}
Not getting what I am doing wrong. Any help, please.
[edit]
: Not getting the label in the backend as well:
Please find the screen-shot attached.
magento2 magento2.2 upgrade-script upgradeschema
magento2 magento2.2 upgrade-script upgradeschema
edited Sep 11 '18 at 5:13
Bhakti Thakkar
51913
51913
asked Jun 21 '18 at 8:47
Narendra
10710
10710
have You use 'php bin/magento setup:upgrade' ?
– Konrad Siamro
Jun 21 '18 at 8:50
yes i did, but i think that their is some another issue as when in upgrade function for debug i did echo 'hi'; exit;. Their came no error in setup:upgrade
– Narendra
Jun 21 '18 at 8:58
I am a bit confused that what i should use installdata or upgradedata
– Narendra
Jun 21 '18 at 8:59
I got the point. Actually we use upgradedata when module is already installed but module version should also change but i was not changing the module version so the changes were not reflecting. Thanks by the way
– Narendra
Jun 21 '18 at 9:03
Please check my updated answer.
– Rohan Hapani
Jun 21 '18 at 9:31
add a comment |
have You use 'php bin/magento setup:upgrade' ?
– Konrad Siamro
Jun 21 '18 at 8:50
yes i did, but i think that their is some another issue as when in upgrade function for debug i did echo 'hi'; exit;. Their came no error in setup:upgrade
– Narendra
Jun 21 '18 at 8:58
I am a bit confused that what i should use installdata or upgradedata
– Narendra
Jun 21 '18 at 8:59
I got the point. Actually we use upgradedata when module is already installed but module version should also change but i was not changing the module version so the changes were not reflecting. Thanks by the way
– Narendra
Jun 21 '18 at 9:03
Please check my updated answer.
– Rohan Hapani
Jun 21 '18 at 9:31
have You use 'php bin/magento setup:upgrade' ?
– Konrad Siamro
Jun 21 '18 at 8:50
have You use 'php bin/magento setup:upgrade' ?
– Konrad Siamro
Jun 21 '18 at 8:50
yes i did, but i think that their is some another issue as when in upgrade function for debug i did echo 'hi'; exit;. Their came no error in setup:upgrade
– Narendra
Jun 21 '18 at 8:58
yes i did, but i think that their is some another issue as when in upgrade function for debug i did echo 'hi'; exit;. Their came no error in setup:upgrade
– Narendra
Jun 21 '18 at 8:58
I am a bit confused that what i should use installdata or upgradedata
– Narendra
Jun 21 '18 at 8:59
I am a bit confused that what i should use installdata or upgradedata
– Narendra
Jun 21 '18 at 8:59
I got the point. Actually we use upgradedata when module is already installed but module version should also change but i was not changing the module version so the changes were not reflecting. Thanks by the way
– Narendra
Jun 21 '18 at 9:03
I got the point. Actually we use upgradedata when module is already installed but module version should also change but i was not changing the module version so the changes were not reflecting. Thanks by the way
– Narendra
Jun 21 '18 at 9:03
Please check my updated answer.
– Rohan Hapani
Jun 21 '18 at 9:31
Please check my updated answer.
– Rohan Hapani
Jun 21 '18 at 9:31
add a comment |
3 Answers
3
active
oldest
votes
Check if you have updated module version in module.xml
and composer.json
file before running php bin/magento setup:upgrade
.
Can you please have a look at my updated question and help me in not getting labels in backend. Please ?
– Narendra
Jun 21 '18 at 9:53
add a comment |
You can create custom attribute using InstallData.php file on below path :
/app/code/CompanyName/ModuleName/Setup/
use MagentoCustomerModelCustomer;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkAppResourceConnection;
use MagentoEavModelEntityAttributeSourceBoolean;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @var MagentoIndexerModelIndexerFactory
*/
protected $indexerFactory;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory,
ResourceConnection $resource,
MagentoFrameworkIndexerIndexerInterfaceFactory $indexerFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->resource = $resource;
$this->indexerFactory = $indexerFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'is_allow', [
'type' => 'int',
'label' => 'Allow',
'input' => 'select',
'source' => Boolean::class,
'required' => true,
'default' => '1',
'sort_order' => 300,
'user_defined' => true,
'system' => false,
'position' => 400,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_allow')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'], //you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
Then, php bin/magento s:up execute this command and cache clean.
Hi, actually the script that I mentioned above in my question worked for me after changing module version. But dont know why I am not getting lables in backend .. I am getting only field box and that also on the top. Can you please help me in this
– Narendra
Jun 21 '18 at 9:49
It's better and proper way for add attribute at install time using InstallData. It's magento proper way.
– Rohan Hapani
Jun 21 '18 at 10:19
Set latest version in module.xml file and upgrade module.
– Rohan Hapani
Jun 21 '18 at 10:24
add a comment |
Unless you haven't copy/pasted your whole file, I think you may be missing a namespace. Without it, the UpgradeData script will not run.
namespace YourVendorYourModuleSetup;
before your use
statements.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f230805%2fmagento-2-upgradedata-php-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Check if you have updated module version in module.xml
and composer.json
file before running php bin/magento setup:upgrade
.
Can you please have a look at my updated question and help me in not getting labels in backend. Please ?
– Narendra
Jun 21 '18 at 9:53
add a comment |
Check if you have updated module version in module.xml
and composer.json
file before running php bin/magento setup:upgrade
.
Can you please have a look at my updated question and help me in not getting labels in backend. Please ?
– Narendra
Jun 21 '18 at 9:53
add a comment |
Check if you have updated module version in module.xml
and composer.json
file before running php bin/magento setup:upgrade
.
Check if you have updated module version in module.xml
and composer.json
file before running php bin/magento setup:upgrade
.
answered Jun 21 '18 at 9:04
mighty_hk
4459
4459
Can you please have a look at my updated question and help me in not getting labels in backend. Please ?
– Narendra
Jun 21 '18 at 9:53
add a comment |
Can you please have a look at my updated question and help me in not getting labels in backend. Please ?
– Narendra
Jun 21 '18 at 9:53
Can you please have a look at my updated question and help me in not getting labels in backend. Please ?
– Narendra
Jun 21 '18 at 9:53
Can you please have a look at my updated question and help me in not getting labels in backend. Please ?
– Narendra
Jun 21 '18 at 9:53
add a comment |
You can create custom attribute using InstallData.php file on below path :
/app/code/CompanyName/ModuleName/Setup/
use MagentoCustomerModelCustomer;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkAppResourceConnection;
use MagentoEavModelEntityAttributeSourceBoolean;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @var MagentoIndexerModelIndexerFactory
*/
protected $indexerFactory;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory,
ResourceConnection $resource,
MagentoFrameworkIndexerIndexerInterfaceFactory $indexerFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->resource = $resource;
$this->indexerFactory = $indexerFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'is_allow', [
'type' => 'int',
'label' => 'Allow',
'input' => 'select',
'source' => Boolean::class,
'required' => true,
'default' => '1',
'sort_order' => 300,
'user_defined' => true,
'system' => false,
'position' => 400,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_allow')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'], //you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
Then, php bin/magento s:up execute this command and cache clean.
Hi, actually the script that I mentioned above in my question worked for me after changing module version. But dont know why I am not getting lables in backend .. I am getting only field box and that also on the top. Can you please help me in this
– Narendra
Jun 21 '18 at 9:49
It's better and proper way for add attribute at install time using InstallData. It's magento proper way.
– Rohan Hapani
Jun 21 '18 at 10:19
Set latest version in module.xml file and upgrade module.
– Rohan Hapani
Jun 21 '18 at 10:24
add a comment |
You can create custom attribute using InstallData.php file on below path :
/app/code/CompanyName/ModuleName/Setup/
use MagentoCustomerModelCustomer;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkAppResourceConnection;
use MagentoEavModelEntityAttributeSourceBoolean;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @var MagentoIndexerModelIndexerFactory
*/
protected $indexerFactory;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory,
ResourceConnection $resource,
MagentoFrameworkIndexerIndexerInterfaceFactory $indexerFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->resource = $resource;
$this->indexerFactory = $indexerFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'is_allow', [
'type' => 'int',
'label' => 'Allow',
'input' => 'select',
'source' => Boolean::class,
'required' => true,
'default' => '1',
'sort_order' => 300,
'user_defined' => true,
'system' => false,
'position' => 400,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_allow')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'], //you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
Then, php bin/magento s:up execute this command and cache clean.
Hi, actually the script that I mentioned above in my question worked for me after changing module version. But dont know why I am not getting lables in backend .. I am getting only field box and that also on the top. Can you please help me in this
– Narendra
Jun 21 '18 at 9:49
It's better and proper way for add attribute at install time using InstallData. It's magento proper way.
– Rohan Hapani
Jun 21 '18 at 10:19
Set latest version in module.xml file and upgrade module.
– Rohan Hapani
Jun 21 '18 at 10:24
add a comment |
You can create custom attribute using InstallData.php file on below path :
/app/code/CompanyName/ModuleName/Setup/
use MagentoCustomerModelCustomer;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkAppResourceConnection;
use MagentoEavModelEntityAttributeSourceBoolean;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @var MagentoIndexerModelIndexerFactory
*/
protected $indexerFactory;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory,
ResourceConnection $resource,
MagentoFrameworkIndexerIndexerInterfaceFactory $indexerFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->resource = $resource;
$this->indexerFactory = $indexerFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'is_allow', [
'type' => 'int',
'label' => 'Allow',
'input' => 'select',
'source' => Boolean::class,
'required' => true,
'default' => '1',
'sort_order' => 300,
'user_defined' => true,
'system' => false,
'position' => 400,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_allow')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'], //you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
Then, php bin/magento s:up execute this command and cache clean.
You can create custom attribute using InstallData.php file on below path :
/app/code/CompanyName/ModuleName/Setup/
use MagentoCustomerModelCustomer;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkAppResourceConnection;
use MagentoEavModelEntityAttributeSourceBoolean;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @var MagentoIndexerModelIndexerFactory
*/
protected $indexerFactory;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory,
ResourceConnection $resource,
MagentoFrameworkIndexerIndexerInterfaceFactory $indexerFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->resource = $resource;
$this->indexerFactory = $indexerFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'is_allow', [
'type' => 'int',
'label' => 'Allow',
'input' => 'select',
'source' => Boolean::class,
'required' => true,
'default' => '1',
'sort_order' => 300,
'user_defined' => true,
'system' => false,
'position' => 400,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_allow')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'], //you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
Then, php bin/magento s:up execute this command and cache clean.
answered Jun 21 '18 at 9:31
Rohan Hapani
5,83721662
5,83721662
Hi, actually the script that I mentioned above in my question worked for me after changing module version. But dont know why I am not getting lables in backend .. I am getting only field box and that also on the top. Can you please help me in this
– Narendra
Jun 21 '18 at 9:49
It's better and proper way for add attribute at install time using InstallData. It's magento proper way.
– Rohan Hapani
Jun 21 '18 at 10:19
Set latest version in module.xml file and upgrade module.
– Rohan Hapani
Jun 21 '18 at 10:24
add a comment |
Hi, actually the script that I mentioned above in my question worked for me after changing module version. But dont know why I am not getting lables in backend .. I am getting only field box and that also on the top. Can you please help me in this
– Narendra
Jun 21 '18 at 9:49
It's better and proper way for add attribute at install time using InstallData. It's magento proper way.
– Rohan Hapani
Jun 21 '18 at 10:19
Set latest version in module.xml file and upgrade module.
– Rohan Hapani
Jun 21 '18 at 10:24
Hi, actually the script that I mentioned above in my question worked for me after changing module version. But dont know why I am not getting lables in backend .. I am getting only field box and that also on the top. Can you please help me in this
– Narendra
Jun 21 '18 at 9:49
Hi, actually the script that I mentioned above in my question worked for me after changing module version. But dont know why I am not getting lables in backend .. I am getting only field box and that also on the top. Can you please help me in this
– Narendra
Jun 21 '18 at 9:49
It's better and proper way for add attribute at install time using InstallData. It's magento proper way.
– Rohan Hapani
Jun 21 '18 at 10:19
It's better and proper way for add attribute at install time using InstallData. It's magento proper way.
– Rohan Hapani
Jun 21 '18 at 10:19
Set latest version in module.xml file and upgrade module.
– Rohan Hapani
Jun 21 '18 at 10:24
Set latest version in module.xml file and upgrade module.
– Rohan Hapani
Jun 21 '18 at 10:24
add a comment |
Unless you haven't copy/pasted your whole file, I think you may be missing a namespace. Without it, the UpgradeData script will not run.
namespace YourVendorYourModuleSetup;
before your use
statements.
add a comment |
Unless you haven't copy/pasted your whole file, I think you may be missing a namespace. Without it, the UpgradeData script will not run.
namespace YourVendorYourModuleSetup;
before your use
statements.
add a comment |
Unless you haven't copy/pasted your whole file, I think you may be missing a namespace. Without it, the UpgradeData script will not run.
namespace YourVendorYourModuleSetup;
before your use
statements.
Unless you haven't copy/pasted your whole file, I think you may be missing a namespace. Without it, the UpgradeData script will not run.
namespace YourVendorYourModuleSetup;
before your use
statements.
answered yesterday
Morgon
1508
1508
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f230805%2fmagento-2-upgradedata-php-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
have You use 'php bin/magento setup:upgrade' ?
– Konrad Siamro
Jun 21 '18 at 8:50
yes i did, but i think that their is some another issue as when in upgrade function for debug i did echo 'hi'; exit;. Their came no error in setup:upgrade
– Narendra
Jun 21 '18 at 8:58
I am a bit confused that what i should use installdata or upgradedata
– Narendra
Jun 21 '18 at 8:59
I got the point. Actually we use upgradedata when module is already installed but module version should also change but i was not changing the module version so the changes were not reflecting. Thanks by the way
– Narendra
Jun 21 '18 at 9:03
Please check my updated answer.
– Rohan Hapani
Jun 21 '18 at 9:31