Question

i've a extension to install, the following code is in setup file

$installer->addAttribute('customer', 'facebook_uid', array(
        'type'   => 'varchar',
        'label'     => 'Facebook Uid',
        'visible'   => false,
        'required'  => false
));

but the code is conflict with existing database, i have to add this manually via backend or phpmyadmin

Was it helpful?

Solution

So first and foremost please do not add items via mysql as this could cause issues

Secondly if you want add attributes to the customer without creating a script then there are some extensions that do that

Finally, actually this is not such a bad question if you simply want to know what goes on "under the hood" when adding attributes so I will try and answer this for you.

Gets the entity type id

First it finds out which entity type we are dealing with. With the default data customer will be 1.

$entityTypeId = $this->getEntityTypeId($entityTypeId);

Prepares the data

Then from all the information you have given the function it set-up an array of all the data, for your example it looks as follows.

array(23) {
  ["entity_type_id"]=>
  string(1) "1"
  ["attribute_code"]=>
  string(12) "facebook_uid"
  ["backend_model"]=>
  NULL
  ["backend_type"]=>
  string(7) "varchar"
  ["backend_table"]=>
  NULL
  ["frontend_model"]=>
  NULL
  ["frontend_input"]=>
  string(4) "text"
  ["frontend_label"]=>
  string(12) "Facebook Uid"
  ["frontend_class"]=>
  NULL
  ["source_model"]=>
  NULL
  ["is_required"]=>
  int(0)
  ["is_user_defined"]=>
  int(0)
  ["default_value"]=>
  NULL
  ["is_unique"]=>
  int(0)
  ["note"]=>
  NULL
  ["is_global"]=>
  int(1)
  ["is_visible"]=>
  int(0)
  ["is_system"]=>
  int(1)
  ["input_filter"]=>
  NULL
  ["multiline_count"]=>
  int(0)
  ["validate_rules"]=>
  NULL
  ["data_model"]=>
  NULL
  ["sort_order"]=>
  int(0)
}

Validates the data

Then it validates the data and moves onto the insert/update

$attributeId = $this->getAttribute($entityTypeId, $code, 'attribute_id');
if ($attributeId) {
    $this->updateAttribute($entityTypeId, $attributeId, $data, null, $sortOrder);
} else {
    $this->_insertAttribute($data);
}

So let's assume we are doing an insert case in my case we are ;) Now we get down to our first sql query, YEAH!, and it looks like this.

INSERT INTO
  `eav_attribute` (
    `entity_type_id`,
    `attribute_code`,
    `backend_model`,
    `backend_type`,
    `backend_table`,
    `frontend_model`,
    `frontend_input`,
    `frontend_label`,
    `frontend_class`,
    `source_model`,
    `is_required`,
    `is_user_defined`,
    `default_value`,
    `is_unique`,
    `note`
  ) VALUES (
    1,
    "facebook_uid",
    NULL,
    "varchar",
    NULL,
    NULL,
    "text",
    "Facebook Uid",
    NULL,
    NULL,
    0,
    0,
    NULL,
    0,
    NULL
)

It the takes the last insert id and performs another insert to additional tables based on the attribute entity type. In our case the customer_eav_attribute where 967 is the attribute_id that we just inserted.

INSERT INTO
  `customer_eav_attribute` (
    `attribute_id`,
    `is_visible`,
    `is_system`,
    `input_filter`, 
    `multiline_count`, 
    `validate_rules`, 
    `data_model`, 
    `sort_order`
  ) VALUES (
    967,
    0,
    1,
    NULL,
    0,
    NULL,
    NULL,
    0
  )

Adds attribute to set

As we continue into how it adds attributes you see the attribute gets added to a group or set. In our case just a set, so let's see what happens to the database.

So first we check to see if the set attached to the attribute with the following select.

SELECT
    `eav_entity_attribute`.*
FROM
    `eav_entity_attribute`
WHERE
    (attribute_set_id = 1)
    AND (attribute_id = 967)

But since we have nothing returned from this select we move onto another insert which looks like this:

INSERT INTO
  `eav_entity_attribute` (
    `entity_type_id`, 
    `attribute_set_id`, 
    `attribute_group_id`, 
    `attribute_id`, 
    `sort_order`
  ) VALUES (
    1,
    1,
    1,
    967,
    111
  )

Adds options

Finally it then adds any attribute options if we have any, but luck us we don't have any cause I am a bit sick of sql now.

if (isset($attr['option']) && is_array($attr['option'])) {
    $option = $attr['option'];
    $option['attribute_id'] = $this->getAttributeId($entityTypeId, $code);
    $this->addAttributeOption($option);
}

Final Note

And that is it all the sql you could wish for it you really wanted to do it that way, but please please please do not do it via direct sql and please either create a script or install an extension for this, but if you do want to do it via sql then make sure you have a backup before you start :)

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