1

I have created a simple module that should add a custom attribute Display Name to customers but when i add this to my site i get no attribute show up. The module is correctly setup and enabled

I've looked in the eav_attribute table in the database and theres no record of it. Cache has been flushed before and after

etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Namespace_Module>
      <version>0.0.1</version>
    </Namespace_Module>
  </modules>
    <global>
      <resources>
          <module_setup>
            <setup>
              <module>Namespace_Module</module>
              <class>Mage_Eav_Model_Entity_Setup</class>
            </setup>
            <connection>
              <use>default_setup</use>
            </connection>
          </module_setup>
      </resources>
    </global>
</config>

sql/module_setup/mysql4-install-0.0.1.php

<?php
$installer = $this;
$installer->startSetup();
$customerEntityTypeId = $installer->getEntityTypeId('customer');
$atttributeCode = 'display_name';

$installer->removeAttribute($customerEntityTypeId, $atttributeCode );
$installer->addAttribute('customer', $atttributeCode, array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'Display Name',
    'global' => true,
    'visible' => true,
    'required' => false,
    'user_defined' => true,
    'visible_on_front' => true
));

$usedInCustomerAddressForms = array(
    'adminhtml_customer',
    'customer_account_edit'
);
$attribute = Mage::getSingleton('eav/config')->getAttribute('customer', $attributeCode);
$attribute->setData('used_in_forms', $usedInCustomerAddressForms);
$attribute->save();
$this->endSetup();
5
  • But can you find the attribute in eav_attribute table?
    – Marius
    Commented Aug 22, 2014 at 12:03
  • are you sure, your installer script file is accessing by magento ? check for an entry module_setup in core_resource table Commented Aug 22, 2014 at 12:06
  • Looking in the eav_attribute table there are no new entries for this (have also searched). Checking the core_resource table i can see the customerattribute_setup entry (customer attribute is the actual name i just used module in my example above)
    – odd_duck
    Commented Aug 22, 2014 at 12:10
  • Delete the record from core_resource and try again. But make sure there is no other module with this alias for the setup.
    – Marius
    Commented Aug 22, 2014 at 12:10
  • Have just deleted the record and trying again and this time it does insert. It does not show in my admin and edit forms though like i stated in the code. Do i need to make changes to allow this or should it be picked up automatically? (i did similar for custom category attributes and they showed in admin straight away without having to add further code in the template files)
    – odd_duck
    Commented Aug 22, 2014 at 12:17

2 Answers 2

3

The first param of addAttribute() should not be the string "customer", it should be the entity type id, which you already have in $customerEntityTypeId.

The same for the function getAttribute().

So change

$installer->addAttribute('customer', $atttributeCode, array(

to

$installer->addAttribute($customerEntityTypeId, $atttributeCode, array(

and change

$attribute = Mage::getSingleton('eav/config')->getAttribute('customer', $attributeCode);

to

$attribute = Mage::getSingleton('eav/config')->getAttribute($customerEntityTypeId, $attributeCode);
2
  • it works with the entity type code also, not only the entity type id
    – Marius
    Commented Aug 22, 2014 at 11:54
  • just tried the above and still no luck
    – odd_duck
    Commented Aug 22, 2014 at 12:11
1

Your resource setup seems to be wrong.

  <resources>
      <module_setup>
        <setup>
          <module>Namespace_Module</module>
          <class>Namespace_Module_Model_Resource_Setup</class>
        </setup>
        <connection>
          <use>default_setup</use>
        </connection>
      </module_setup>
  </resources>

See the class node. You were using Mage_Eav_Model_Entity_Setup and it is wrong. If you are planning to use your Magento's default resource setup, then you don't want to define this part. Setting up a resource setup for a custom module allows us more power than rely on default setup resource. If you are planning to use your own resource setup, then it is essential to define setup class. It should look like this.

<?php
class Namespace_Module_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup {
} 

However I don't think, this is the real cause of your issue. But I need to point out this because, according to me, this part seems wrong.

Workaround

You can do a small workaround to find the issue. It is sure that the problem inside installer script. So what you need to do is, delete the entry that corresponds for your module from core_resource table. This will make Magento again execute the installer file. Put a die() statement in the installer file and analyze each and every item inside that file by printing out it.

There is an excellent tutorial for this. Try to follow this This

4
  • For sure this is not the issue. Having a class of your own that just extends Mage_Eav_Model_Entity_Setup has the same effect as using the base class.
    – Marius
    Commented Aug 22, 2014 at 12:02
  • @Marius :Yes I know this is not the real cause. I really pointed out it in my answer itself. According to config setup, it appears that, he doesn't know what resource setup does actually. That is why I just pointed it out. Is that a crime ? :) Commented Aug 22, 2014 at 12:04
  • Happy for all the help and advice :) I'm still getting to grips with magento. May i ask which file the setup class php code should be in? Helper, Model or controller?
    – odd_duck
    Commented Aug 22, 2014 at 12:11
  • see my update. It may help to dig out the problem Commented Aug 22, 2014 at 12:18

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.