Magento - добавить атрибут к объекту клиента
В течение двух дней я пытаюсь добавить новый атрибут клиента в базу данных magento. Но он не работает. Я добавил простое поле "ник" в register.phtml и edit.phtml следующее руководство:
http://www.magentocommerce.com/wiki/5_-_modules_and_development/customers_and_accounts/registration_fields
Вот неотъемлемая статья форума:
http://www.magentocommerce.com/boards/viewthread/9620/
Атрибут добавляется в таблицу базы данных eav_attribute, но значение не отображается в customer_entity_varchar.
Вот что у меня есть:
Все файлы находятся в разделе app/code/local/Company/
Приложение/код/локальные/Компания/Клиент/и т.д. /config.xml
http://mysticpaste.com/view/9466 (обратите внимание: <!-- nickname -->
)
app/code/local/Company/Customer/Model/Entity/Setup.php (обратите внимание: /* nickname */
)
http://mysticpaste.com/view/9467
class Company_Customer_Model_Entity_Setup extends Mage_Customer_Model_Entity_Setup{
public function getDefaultEntities()
{
return array(
'customer' => array(
'entity_model' =>'customer/customer',
'table' => 'customer/entity',
'increment_model' => 'eav/entity_increment_numeric',
'increment_per_store' => false,
'additional_attribute_table' => 'customer/eav_attribute',
'entity_attribute_collection' => 'customer/attribute_collection',
'attributes' => array(
// 'entity_id' => array('type'=>'static'),
// 'entity_type_id' => array('type'=>'static'),
// 'attribute_set_id' => array('type'=>'static'),
// 'increment_id' => array('type'=>'static'),
// 'created_at' => array('type'=>'static'),
// 'updated_at' => array('type'=>'static'),
// 'is_active' => array('type'=>'static'),
'website_id' => array(
'type' => 'static',
'label' => 'Associate to Website',
'input' => 'select',
'source' => 'customer/customer_attribute_source_website',
'backend' => 'customer/customer_attribute_backend_website',
'sort_order' => 10,
),
'store_id' => array(
'type' => 'static',
'label' => 'Create In',
'input' => 'select',
'source' => 'customer/customer_attribute_source_store',
'backend' => 'customer/customer_attribute_backend_store',
'visible' => false,
'sort_order' => 20,
),
'created_in' => array(
'type' => 'varchar',
'label' => 'Created From',
'sort_order' => 30,
),
'prefix' => array(
'label' => 'Prefix',
'required' => false,
'sort_order' => 37,
),
'firstname' => array(
'label' => 'First Name',
'sort_order' => 40,
),
'middlename' => array(
'label' => 'Middle Name/Initial',
'required' => false,
'sort_order' => 43,
),
'lastname' => array(
'label' => 'Last Name',
'sort_order' => 50,
),
'suffix' => array(
'label' => 'Suffix',
'required' => false,
'sort_order' => 53,
),
'email' => array(
'type' => 'static',
'label' => 'Email',
'class' => 'validate-email',
'sort_order' => 60,
), /* nickname */
'nickname' => array(
'label' => 'nickname',
'unique' => true,
'sort_order' => 65,
'required' => true,
),
'group_id' => array(
'type' => 'static',
'input' => 'select',
'label' => 'Group',
'source' => 'customer/customer_attribute_source_group',
'sort_order' => 70,
),
'dob' => array(
'type' => 'datetime',
'input' => 'date',
'backend' => 'eav/entity_attribute_backend_datetime',
'required' => false,
'label' => 'Date Of Birth',
'sort_order' => 80,
),
'password_hash' => array(
'input' => 'hidden',
'backend' => 'customer/customer_attribute_backend_password',
'required' => false,
),
'default_billing' => array(
'type' => 'int',
'visible' => false,
'required' => false,
'backend' => 'customer/customer_attribute_backend_billing',
),
'default_shipping' => array(
'type' => 'int',
'visible' => false,
'required' => false,
'backend' => 'customer/customer_attribute_backend_shipping',
),
'taxvat' => array(
'label' => 'Tax/VAT Number',
'visible' => true,
'required' => false,
),
'confirmation' => array(
'label' => 'Is Confirmed',
'visible' => false,
'required' => false,
),
'created_at' => array(
'type' => 'static',
'label' => 'Created At',
'visible' => false,
'required' => false,
'input' => 'date',
),
),
),
'customer_address'=>array(
'entity_model' =>'customer/customer_address',
'table' => 'customer/address_entity',
'additional_attribute_table' => 'customer/eav_attribute',
'entity_attribute_collection' => 'customer/address_attribute_collection',
'attributes' => array(
// 'entity_id' => array('type'=>'static'),
// 'entity_type_id' => array('type'=>'static'),
// 'attribute_set_id' => array('type'=>'static'),
// 'increment_id' => array('type'=>'static'),
// 'parent_id' => array('type'=>'static'),
// 'created_at' => array('type'=>'static'),
// 'updated_at' => array('type'=>'static'),
// 'is_active' => array('type'=>'static'),
'prefix' => array(
'label' => 'Prefix',
'required' => false,
'sort_order' => 7,
),
'firstname' => array(
'label' => 'First Name',
'sort_order' => 10,
),
'middlename' => array(
'label' => 'Middle Name/Initial',
'required' => false,
'sort_order' => 13,
),
'lastname' => array(
'label' => 'Last Name',
'sort_order' => 20,
),
'suffix' => array(
'label' => 'Suffix',
'required' => false,
'sort_order' => 23,
),
'company' => array(
'label' => 'Company',
'required' => false,
'sort_order' => 30,
),
'street' => array(
'type' => 'text',
'backend' => 'customer_entity/address_attribute_backend_street',
'input' => 'multiline',
'label' => 'Street Address',
'sort_order' => 40,
),
'city' => array(
'label' => 'City',
'sort_order' => 50,
),
'country_id' => array(
'type' => 'varchar',
'input' => 'select',
'label' => 'Country',
'class' => 'countries',
'source' => 'customer_entity/address_attribute_source_country',
'sort_order' => 60,
),
'region' => array(
'backend' => 'customer_entity/address_attribute_backend_region',
'label' => 'State/Province',
'class' => 'regions',
'sort_order' => 70,
),
'region_id' => array(
'type' => 'int',
'input' => 'hidden',
'source' => 'customer_entity/address_attribute_source_region',
'required' => 'false',
'sort_order' => 80,
'label' => 'State/Province'
),
'postcode' => array(
'label' => 'Zip/Postal Code',
'sort_order' => 90,
),
'telephone' => array(
'label' => 'Telephone',
'sort_order' => 100,
),
'fax' => array(
'label' => 'Fax',
'required' => false,
'sort_order' => 110,
),
),
),
);
}
}
приложение/дизайн/интерфейс/по умолчанию/компания/шаблон/клиент/форма/register.phtml
<label for="nickname" class="required" style="margin-left: -1px;"><em>*</em><?php echo $this->__('Nickname') ?></label>
<input type="text" name="nickname" id="nickname" value="<?php echo $this->htmlEscape($this->getFormData()->getnickname()) ?>" title="<?php echo $this->__('Nickname') ?>" class="input-text required-entry" />
Я также запускаю этот фрагмент кода php в верхней части register.phtml, чтобы добавить атрибут в eav_attribute
этот код был выполнен один раз:
<?php
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$AttrCode = 'nickname';
$settings = array (
'position' => 1,
'is_required' => 0
);
$setup->addAttribute('1', $AttrCode, $settings);
?>
приложение/дизайн/интерфейс/по умолчанию/компания/шаблон/клиент/форма/edit.phtml
<label for="nickname" class="required" style="margin-left: -1px;"><em>*</em><?php echo $this->__('Nickname') ?></label>
<input type="text" name="nickname" id="nickname" value="<?php echo $this->htmlEscape($this->getCustomer()->getnickname()) ?>" title="<?php echo $this->__('Nickname') ?>" class="input-text required-entry" />
Я также вручную очистил папку /var/cache, но без какого-либо эффекта.
Я понятия не имею, что еще я могу сделать, поэтому любая помощь очень приветствуется.
EDIT:
Хорошо, вот что я пробовал по корню magento:
define('MAGENTO', realpath(dirname(__FILE__)));
ini_set('memory_limit', '32M');
set_time_limit (0);
require_once MAGENTO . '/app/Mage.php';
Mage::app();
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $setup->getEntityTypeId('customer');
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$setup->addAttribute('customer', 'nickname', array(
'input' => 'text',
'type' => 'text',
'label' => 'Nickname',
'visible' => 1,
'required' => 1,
'user_defined' => 1,
));
$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'nickname',
'999' //sort_order
);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'nickname');
$oAttribute->setData('used_in_forms', array('adminhtml_customer'));
$oAttribute->save();
$installer->endSetup();
Этот код будет выполнен отлично, и в сообщении eav_attribute не будет добавлено никакого сообщения об ошибке или чего-то другого, кроме нового атрибута.
Ответы
Ответ 1
Я отредактировал ваш script, и он отлично работал у меня, теперь я вижу новый атрибут.
Замените это:
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
При этом:
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->startSetup();