0

I want to add new column, let's call it test, to customer_entity table.

I managed to add the column to the table but values aren't saved for this column when I write something like:

$customer->setData("test", "something");
$customer->save();

The code I used to add the new column in my module's InstallSchema.php:

$eavTable = $installer->getTable('customer_entity');
$columns = [
    'test' => [
        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        'length' => '6',
        'nullable' => false,
        'default' => null,
        'comment' => 'test',
    ]
];
foreach ($columns as $name => $definition) {
    $connection->addColumn($eavTable, $name, $definition);
}
$installer->endSetup();
5
  • first check if column is installed into the table, also let us know the event and observer you are catching.. Commented May 10, 2016 at 6:22
  • this column was installed success in table customer_entity .
    – Junior
    Commented May 10, 2016 at 6:28
  • can you please let us know the observer/event you are calling ?? Commented May 10, 2016 at 6:35
  • @Junior you need to rewrite customer entity class. Currently, I have no code to for example, but I have faced the same problem with the review to add a column location and I have to rewrite review class. Search for how to rewrite core class in Magento 2.x.
    – Manish
    Commented May 10, 2016 at 6:49
  • @ Mohammad Mujassam : $objCustomer = $this->objectManager->create('\Magento\Customer\Model\Customer'); $customer = $objCustomer ->load($customerId);
    – Junior
    Commented May 10, 2016 at 6:50

2 Answers 2

1

This method used to work with Magento 1, but not in Magento 2. It's true that Magento 2 uses legacy models (that extend from Magento\Framework\DataObject), but it's not safe to rely on this anymore if you want to create future-proof extensions. You should rely on Service Contracts, Data Models and their added functionality for this.

What you are looking for are indeed EAV attributes (which can be accessed using the getCustomAttribute()-method that is declared in Magento\Customer\Api\Data\CustomerInterface), or extension attributes (which allow third party developers to add extra 'stuff' to data models by using interceptors).

Depending on your needs you have to pick one of those.

Rewriting a core class is (like in Magento 1) a thing you should avoid as much as possible in Magento 2. To get your task done, learn getCustomAttribute() and getExtensionAttribute() and how to use them. Magento added a lot more abstraction in Magento 2 to make it more flexible than Magento 1.

More information: http://devdocs.magento.com/guides/v2.1/extension-dev-guide/attributes.html

0

Hi think that to add an attribute to customer entity is better use eav. Anyway try to see this github issue, it fix a similar your issue (but with eav).

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.