0

I want to add an attribute of customer,like phone number,how to add an attribute for primary I find source like this

$eavSetup->addAttribute(
            Customer::ENTITY,
                'mobile_phone',
                [
                    'group' => 'General',
                    'type' => 'text',
                    'label' => 'Mobile Phone',
                    'input' => 'text',
                    'source' => '',
                    'frontend' => '',
                    'backend' => '',
                    'required' => false,
                    'sort_order' => 66,
                    'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                    'is_used_in_grid' => false,
                    'is_visible_in_grid' => false,
                    'is_filterable_in_grid' => false,
                    'visible' => true,
                    'is_html_allowed_on_front' => true,
                    'visible_on_front' => true
                ]
        );
$CustomerAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'mobile_phone');
        $CustomerAttribute ->addData(
            [
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer', 'customer_account_create']
            ]
        );

but it doesn't primary attribute

3
  • Primary means required field? Aug 5 at 6:41
  • what do you mean by primary ? you want this attribute to be part of the primary key that identify the customer entity ?
    – Claims
    Aug 5 at 9:08
  • @Claims it mean this attribute identify the customer entity
    – DanielHe
    Aug 8 at 2:00

1 Answer 1

0

I think you can use the magento core method to do that (never tried it on my own though)

addPrimaryKeyIfExists from src/vendor/magento/framework/Setup/SchemaListener.php

/**
 * Add primary key if exists in definition.
 *
 * @param string $tableName
 * @param string $columnName
 * @param array $definition
 * @param string $primaryKeyName
 * @return array
 */
private function addPrimaryKeyIfExists($tableName, $columnName, $definition, $primaryKeyName)
{
    if (isset($definition['primary']) && $definition['primary']) {
        $dataToLog['constraints']['primary'][$primaryKeyName] = [
            'type' => 'primary',
            'name' => $primaryKeyName,
            'disabled' => false,
            'columns' => [$columnName => strtolower($columnName)]
        ];

        $this->log($tableName, $dataToLog);
    }

    unset($definition['primary']);
    return $definition;
}

It's a private method called by

/**
 * Add column.
 *
 * @param string $tableName
 * @param string $columnName
 * @param array $definition
 * @param string $primaryKeyName
 * @param string|null $onCreate
 */
public function addColumn($tableName, $columnName, $definition, $primaryKeyName = 'PRIMARY', $onCreate = null)
{
    $definition = $this->castColumnDefinition($definition, $columnName);
    $definition = $this->addPrimaryKeyIfExists($tableName, $columnName, $definition, $primaryKeyName);
    $definition['onCreate'] = $onCreate;
    $dataToLog['columns'][strtolower($columnName)] = $definition;
    $this->log($tableName, $dataToLog);
}

Hoping this will lead you somewhere. That being said i'm note sure about the fact of adding a primary key to a core customer functionnality, it could have much more impact than you though about.

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.