Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

EAV Config Cache Bug: getAttribute() returns false at creating new attribute #3057

Closed
kiatng opened this issue Mar 3, 2023 · 5 comments · Fixed by #3074
Closed

EAV Config Cache Bug: getAttribute() returns false at creating new attribute #3057

kiatng opened this issue Mar 3, 2023 · 5 comments · Fixed by #3074
Labels

Comments

@kiatng
Copy link
Contributor

kiatng commented Mar 3, 2023

Preconditions (*)

Related to issue #2993. When investigating issue #3055, I was able to create a new attribute in ver 20.0.16 with this:

        $store = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        $eavConfig = Mage::getSingleton('eav/config');
        $attributes = array(
            'select_invoice_type' => array(
                'frontend_label'        => 'Identification for Electronic Invoicing',
                'label'                 => 'Ident Type Billing',
                'backend_type'          => 'text',
                'frontend_input'        => 'select',
                'source'                => 'eav/entity_attribute_source_table',
                'is_user_defined'       => 1,
                'is_system'             => 0,
                'is_visible'            => 1,
                'is_required'           => 0,
            )
        );

        foreach ($attributes as $attributeCode => $data) {
            $attribute = $eavConfig->getAttribute('customer_address', $attributeCode);
            $attribute->setWebsite($store->getWebsite());
            $attribute->addData($data);
            $usedInForms = array(
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address',
                'customer_account_create'
            );

            $attribute->setData('used_in_forms', $usedInForms);
            $attribute->save();
        }

However, with ver 20.1.0-rc2 I get

Fatal error: Uncaught Error: Call to a member function setWebsite() on bool in .../Celera/Crib/controllers/PxController.php:55

The line is $attribute->setWebsite($store->getWebsite());.

Steps to reproduce (*)

  1. As described.
  2. Or just examine the output of Mage::getSingleton('eav/config')->getAttribute('customer_address', 'select_invoice_type').

Expected result (*)

  1. To be able to create new attributes as older version without error.
  2. Mage::getSingleton('eav/config')->getAttribute('customer_address', 'select_invoice_type') returns Mage_Customer_Model_Attribute

Actual result (*)

  1. Fatal error in latest ver.
  2. Mage::getSingleton('eav/config')->getAttribute('customer_address', 'select_invoice_type') returns false
@kiatng kiatng added the bug label Mar 3, 2023
@davidhiendl
Copy link
Contributor

davidhiendl commented Mar 3, 2023

This happens because getAttribute no longer returns an empty model when the attribute does not exist. I think it's not a good idea to use this behavior to create an attribute in the first place.

The way core installers add attributes is, for example:

$installer->addAttribute('customer', 'confirmation', [
    'label'    => 'Is confirmed',
    'visible'  => false,
    'required' => false,
]);

@davidhiendl
Copy link
Contributor

davidhiendl commented Mar 3, 2023

@fballiano @kiatng You reviewed my original PR #2993 and probably looked into the issue as well. Perhaps it does make sense to restore the old "behavior" and return a empty model instance from Mage_Eav_Model_Config::getAttribute for compatibility?

@kiatng
Copy link
Contributor Author

kiatng commented Mar 3, 2023

Magento 1 and OpenMage provide more than one way to do things. So, for BC, I vote to restore the "old behaviour" to fix the "bug".

@fballiano
Copy link
Contributor

I guess an empty model is fine, it's used in many places anyway so...

@davidhiendl
Copy link
Contributor

Alright, I will create a PR for it.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants