Question

I'm creating a Referral Program module, and I want to create a custom column inside the customer_entity table. The column is referred_by and it's a foreign key that as a relation with the customer_entity table itself. This column contains the id of the customer that sent the referral_code. How can I implement this? How can I add this field inside the customer_entity table? I try to add an attribute as static type, but the new column doesn't appear inside that table. I need to declere the db_schema.xml?

Was it helpful?

Solution

You can try using following code.

app/code/Anshu/CustomerEdit/registration.php

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Anshu_CustomerEdit',
    __DIR__
);

app/code/Anshu/CustomerEdit/etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Anshu_CustomerEdit" >
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

app/code/Anshu/CustomerEdit/etc/db_schema.xml

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="customer_entity">
        <column xsi:type="int" name="referred_by" padding="10" unsigned="true" nullable="false"
                comment="Referred By"/>
        <constraint xsi:type="foreign" referenceId="CUSTOMER_ENTITY_REFERRED_BY_CUSTOMER_ENTITY_ENTITY_ID" table="customer_entity"
                    column="referred_by" referenceTable="customer_entity" referenceColumn="entity_id" onDelete="CASCADE"/>
    </table>
</schema>

Then run following command to generate db_schema_whitelist.json

bin/magento setup:db-declaration:generate-whitelist --module-name Anshu_CustomerEdit

Then run bin/magento setup:upgrade command.

Here Anshu is the module namespace and CustomerEdit is the module name.

You can modify code according to your requirement.

OTHER TIPS

You can use InstallSchema Script to do that. Kindly refer below code:

<?php

namespace vendor\module\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{
    /**
    * {@inheritdoc}
    */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $table = $setup->getConnection()->newTable(
            $setup->getTable('customer_entity')
            )->addColumn(
            'referred_by',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            255,
            ['identity' => true, 'nullable' => true],
            'Referred By'
            )->setComment("Id of the customer that sent the Referral code");

        $setup->getConnection()->createTable($table);

        $setup->endSetup();
    }
}

Create new customer custom attribute for referral_code while installing referral module. here is a link to follow how to add custom attribute in customer section.

https://www.mageplaza.com/magento-2-module-development/magento-2-add-customer-attribute-programmatically.html

you can fetch custom attribute value from customer object using below code.

$customer->getCustomAttribute('FIELD_NAME')->getValue();

replace FIELD_NAME by your custom attribute name

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top