1

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?

3

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

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.

0

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();
    }
}
  • ok, and then how can I add the field inside the entity model of customer? Because this field then I need to get and set – Mirko Rapisarda Mar 20 at 9:33
  • 1
    @magefms M2.3 should be added via declarative schema even though it acepts, correct me if am wrong – Prathap Gunasekaran Mar 20 at 9:36
  • @MirkoRapisarda Try with customer attribute for your requirement you can't do this via custom field in a table. – Prathap Gunasekaran Mar 20 at 9:37
  • @PrathapGunasekaran, yeah you are right but just giving him an easier way for now since it also works for 2.3. – magefms Mar 20 at 9:40
  • 1
    @PrathapGunasekaran but with custom attribute, I lost the possibility to add a foreign key relationship, for this reason I want to add it as a real column – Mirko Rapisarda Mar 20 at 9:40
0

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

  • but in this way I lost the possibility to add the referenced_by field as a foreign key – Mirko Rapisarda Mar 20 at 9:37

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.