문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top