3

I need to add a new attribute to the customer model, because I need to save a customer input into the database.

I followed the instructions on this answer.

app/code/local/Black/Newsletter2Go/etc/config.xml

<!-- ... -->

<modules>
    <Black_Newsletter2Go>
        <version>0.1.0</version>
    </Black_Newsletter2Go>
</modules>
<global>
    <resources>
        <Black_Newsletter2Go_setup>
            <setup>
                <module>Black_Newsletter2Go</module>
                <class>Mage_Customer_Model_Entity_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </Black_Newsletter2Go_setup>
        <Black_Newsletter2Go_write>
            <connection>
                <use>core_write</use>
            </connection>
        </Black_Newsletter2Go_write>
        <Black_Newsletter2Go_read>
            <connection>
                <use>core_read</use>
            </connection>
        </Black_Newsletter2Go_read>
    </resources>
</global>

<!-- ... -->

app/code/local/Black/Newsletter2Go/sql/black_newsletter2go_setup/install-0.1.0.php

<?php
Mage::Log("---------- install script started ------------"); //Not showing in logfiles
$installer = $this;
$installer->startSetup();

$installer->addAttribute("customer", "customattribute",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Newsletter 2 Go Emails",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default"  => "emails",
    "frontend" => "",
    "unique"   => false,
    "note"     => "Diese Adresse(n) hat der Kunde für die Newsletter2Go Anmeldung genutzt"
));

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'customattribute',
    '999'  //sort_order
);

$used_in_forms = array();
$used_in_forms[] = "customer_account_edit";
$used_in_forms[] = "adminhtml_customer";

$attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "customattribute");
$attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100)
        ;
$attribute->save();

$installer->endSetup();

Then I executed ./n98.magerun-phar sys:setup:run Response: done

I created two controller actions for testing.

app/code/local/Black/Newsletter2Go/controllers/IndexController.php

<?php

class Black_Newsletter2Go_IndexController extends Mage_Core_Controller_Front_Action
{
    public function getAction()
    {
        $session = Mage::getSingleton('customer/session');
        $customerId = $session->getCustomer()->getId();
        $customerModel = Mage::getModel('customer/customer')->load($customerId);

        echo "ID: '" . $customerModel->getId() . "'<br>"
        . "n2g_emails - try 1:'". $customerModel->getCustomattribute() . "'<br>"
        . "n2g_emails - try 2: '". $customerModel->getData("customattribute") . "'<br>"
        . "email: '". $customerModel->getData("email") . "'<br>";

        echo "<pre>";
        print_r(
            $customerModel->getData()
        );
        echo "</pre>";

        echo "<pre>";
        print_r(
            $customerModel->getData("customattribute")
        );
        echo "</pre>";
    }

    public function setAction()
    {
        $session = Mage::getSingleton('customer/session');
        $customerId = $session->getCustomer()->getId();
        $customerModel = Mage::getModel('customer/customer')->load($customerId);

        //$customer->setCustomattribute("value_set_in_set_action");
        $response = $customerModel->setData("customattribute", "value_set_in_set_action");


        echo "<pre>";
        print_r(
            $response
        );
        echo "</pre>";

        $customerModel->save();
    }


}

If I execute the setAction then I get this response:

[_data:protected] => Array
    (
        [entity_id] => 2273
        [entity_type_id] => 1
        [attribute_set_id] => 0
        [website_id] => 1
        [email] => censored@foo.de
        [group_id] => 6
        [increment_id] => 
        [store_id] => 1
        [created_at] => 2018-09-13T18:33:49+02:00
        [updated_at] => 2018-09-14 16:04:13
        [is_active] => 1
        [disable_auto_group_change] => 0
        [created_in] => Default Store View
        [prefix] => Herr
        [firstname] => foo
        [lastname] => bar
        [password_hash] => removed...
        [customer_activated] => 1
        [default_billing] => 2515
        [default_shipping] => 2515
        [unlock_customer] => 0
        [failed_logins] => 0
        [last_failed_login] => 0
        [customattribute] => value_set_in_set_action
    )

[_hasDataChanges:protected] => 1
[_origData:protected] => Array
    (
        [entity_id] => 2273
        [entity_type_id] => 1
        [attribute_set_id] => 0
        [website_id] => 1
        [email] => censored@foo.de
        [group_id] => 6
        [increment_id] => 
        [store_id] => 1
        [created_at] => 2018-09-13T18:33:49+02:00
        [updated_at] => 2018-09-14 16:04:13
        [is_active] => 1
        [disable_auto_group_change] => 0
        [created_in] => Default Store View
        [prefix] => Herr
        [firstname] => foo
        [lastname] => bar
        [password_hash] => removed...
        [customer_activated] => 1
        [default_billing] => 2515
        [default_shipping] => 2515
        [unlock_customer] => 0
        [failed_logins] => 0
        [last_failed_login] => 0
    )

It looks fine. But if I execute the showAction then I get this:

ID: '2273'
customattribute - try 1:''
customattribute - try 2: ''
email: 'censored@foo.de'
Array
(
    [entity_id] => 2273
    [entity_type_id] => 1
    [attribute_set_id] => 0
    [website_id] => 1
    [email] => censored@foo.de
    [group_id] => 6
    [increment_id] => 
    [store_id] => 1
    [created_at] => 2018-09-13T18:33:49+02:00
    [updated_at] => 2018-09-14 16:13:15
    [is_active] => 1
    [disable_auto_group_change] => 0
    [created_in] => Default Store View
    [prefix] => Herr
    [firstname] => foo
    [lastname] => bar
    [password_hash] => removed...
    [customer_activated] => 1
    [default_billing] => 2515
    [default_shipping] => 2515
    [unlock_customer] => 0
    [failed_logins] => 0
    [last_failed_login] => 0
)

So it was not saved!

If I set the data right before I output it...

$customerModel->setCustomattribute("some_value");
echo $customerModel->getData("customattribute");

...then it shows the output: "some_value"

Why is the value not saved in the setAction from my controller? Did the setup failed? How do I know?

I already deleted the entry of my extension in the database table core_resource and flushed the cache, but it makes no difference.

There are no errors in the logfiles.

2

your question reminds me of a Magento 2 question that I have answered to: please see Magento2 : user define customer attribute not save value while create / save from admin

This answer reminds us that it is required to flush cache storage when changing schema in Magento. Because you describe the data is there when you debug, I think it might be worth to flush the cache storage.

| improve this answer | |
  • I already deleted the entry of my extension in the database table core_resource and flushed the cache, but it makes no difference. – Black Sep 17 '18 at 8:41
  • Did you already reindex your attributes ? – Kozame Sep 17 '18 at 9:33
  • How to reindex attributes? – Black Oct 11 '18 at 7:52
  • when running php bin/magento index:reindex, the eav attribute are reindexed. Now, as you may know, Magento has caches (full cache) that you may want to flush. Finally, Magento can use flat catalog. If both steps from above don't give you a solution, you may try to disable flat catalog. Third-party module can sometimes prevent the flat catalog to work well but hopefully the first 2 steps are sufficient – Herve Tribouilloy Oct 11 '18 at 9:54
0

After alot of core debugging of the file app\code\core\Mage\Core\Model\Resource\Setup.php I found out that magento expects the file to be in either data/Companyname_Modulname_setup/ or in sql/Companyname_Modulname_setup/ of your module. At least on Magento 1.9.3.

So in my example sql/Black_Newsletter2Go_setup or data/Black_Newsletter2Go_setup.

And it has to be named mysql4-data-upgrade-OLDVERSION-NEWVERSION.php e.g. mysql4-data-upgrade-0.1.0-0.1.0.php instead of mysql4-install-0.1.0.php

Make sure that you are using the right version number, look it up in your modules etc/config.xml.

That solved my problem. Remember to be logged in in the fronted too, if you test it.


If it still fails, then clear the cache and reload your site. Thanks to @Herve Tribouilloy for the hint.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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