我需要在magento商店中创建4个新的客户属性。我已经创建了一个模块,如下所示:
Customerattribute >
etc > config.xml
Model > Mysql4 > Setup.php
sql > customerattribute_setup > mysql4-install-0.0.1.php
config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Custom_Customerattribute>
<version>0.0.1</version>
</Custom_Customerattribute>
</modules>
<global>
<resources>
<customerattribute_setup>
<setup>
<module>Custom_Customerattribute</module>
<class>Custom_Customerattribute_Model_Mysql4_Setup</class>
</setup>
....
</customerattribute_setup>
</resources>
</global>
</config>
Setup.php
class Custom_Customerattribute_Model_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
/**
* This method returns true if the attribute exists.
*
* @param string|int $entityTypeId
* @param string|int $attributeId
* @return bool
*/
public function attributeExists($entityTypeId, $attributeId)
{
try
{
$entityTypeId = $this->getEntityTypeId($entityTypeId);
$attributeId = $this->getAttributeId($entityTypeId, $attributeId);
return !empty($attributeId);
}
catch(Exception $e)
{
return FALSE;
}
}
}
mysql4安装-0.0.1.php
$installer = $this;
$installer->startSetup();
$entity = $installer->getEntityTypeId('customer');
if(!$installer->attributeExists($entity, 'paypal')) {
$installer->removeAttribute($entity, 'paypal');
}
$installer->addAttribute($entity, 'paypal', array(
'type' => 'text',
'label' => 'Paypal',
'input' => 'text',
'visible' => TRUE,
'required' => FALSE,
'default_value' => '',
'adminhtml_only' => '0'
));
$forms = array(
'adminhtml_customer',
'customer_account_edit'
);
$attribute = Mage::getSingleton('eav/config')->getAttribute($installer->getEntityTypeId('customer'), 'paypal');
$attribute->setData('used_in_forms', $forms);
$attribute->save();
$installer->endSetup();
这适用于paypal
的第一个客户属性,但我现在希望能够添加其他3个。我希望如果我更改mysql4-install-0.0.1.php
文件来说明这一点:
$installer = $this;
$installer->startSetup();
$entity = $installer->getEntityTypeId('customer');
if(!$installer->attributeExists($entity, 'attribute_2')) {
$installer->removeAttribute($entity, 'attribute_2');
}
$installer->addAttribute($entity, 'attribute_2', array(
'type' => 'text',
'label' => 'Attribute 2',
'input' => 'text',
'visible' => TRUE,
'required' => FALSE,
'default_value' => '',
'adminhtml_only' => '0'
));
$forms = array(
'adminhtml_customer',
'customer_account_edit'
);
$attribute = Mage::getSingleton('eav/config')->getAttribute($installer->getEntityTypeId('customer'), 'attribute_2');
$attribute->setData('used_in_forms', $forms);
$attribute->save();
$installer->endSetup();
并上传新文件并返回到网站attribute_2
将被添加,但事实并非如此。
为什么这只能工作一次但不能再工作?
答案 0 :(得分:10)
您需要添加新文件(安装程序脚本),文件名应为
mysql4-upgrade-0.0.2-0.0.1.php
现在,您可以添加安装程序脚本,如
$installer = $this;
$installer->startSetup();
$entity = $installer->getEntityTypeId('customer');
if(!$installer->attributeExists($entity, 'attribute_2')) {
$installer->removeAttribute($entity, 'attribute_2');
}
$installer->addAttribute($entity, 'attribute_2', array(
'type' => 'text',
'label' => 'Attribute 2',
'input' => 'text',
'visible' => TRUE,
'required' => FALSE,
'default_value' => '',
'adminhtml_only' => '0'
));
$forms = array(
'adminhtml_customer',
'customer_account_edit'
);
$attribute = Mage::getSingleton('eav/config')->getAttribute($installer->getEntityTypeId('customer'), 'attribute_2');
$attribute->setData('used_in_forms', $forms);
$attribute->save();
$installer->endSetup();
您还需要更新config.xml文件。所以它应该是,
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Custom_Customerattribute>
<version>0.0.2</version>
</Custom_Customerattribute>
</modules>
<global>
<resources>
<customerattribute_setup>
<setup>
<module>Custom_Customerattribute</module>
<class>Custom_Customerattribute_Model_Mysql4_Setup</class>
</setup>
....
</customerattribute_setup>
</resources>
</global>
</config>
有关详细信息,请转到 here 。如果您有任何疑问,请在此处发表评论。
<强>更新强> 对不起文件名应该是这样的,
mysql4-upgrade-0.0.2-0.0.1.php
core_resource
表包含模块条目。现在0.0.2
也已更新。所以magento不会查找要加载的模块(更新的)xml文件。所以你需要再次将文件名更改为mysql4-upgrade-0.0.3-0.0.2.php
或删除数据库中的条目,并将模块版本重命名为新的mysql4-upgrade-0.0.0.php
<强>更新-2:强>
这里我附上了新代码,我在当地检查了它的工作正常,
应用程序/代码/本地/包/ MODULENAME的/ etc / config.xml中
<?xml version="1.0"?>
<config>
<modules>
<Packagename_Modulename>
<version>0.0.0</version>
</Packagename_Modulename>
</modules>
<global>
<helpers>
<modulename>
<class>Packagename_Modulename_Helper</class>
</modulename>
</helpers>
<models>
<modulename>
<class>Packagename_Modulename_Model</class>
<resourceModel>modulename_mysql4</resourceModel>
</modulename>
</models>
<resources>
<customerattribute1415104755_setup>
<setup>
<module>Packagename_Modulename</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customerattribute1415104755_setup>
<customerattribute1415104755_write>
<connection>
<use>core_write</use>
</connection>
</customerattribute1415104755_write>
<customerattribute1415104755_read>
<connection>
<use>core_read</use>
</connection>
</customerattribute1415104755_read>
</resources>
</global>
</config>
应用程序/代码/本地/包/ MODULENAME /助手/ Data.php
<?php
class Packagename_Modulename_Helper_Data extends Mage_Core_Helper_Abstract
{
}
应用程序/代码/本地/包/ MODULENAME / SQL / customerattribute1415104755_setup / mysql4安装-0.0.0.php
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("customer", "myattrbute1", array(
"type" => "varchar",
"backend" => "",
"label" => "My attribute-1",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute1");
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();
$installer->addAttribute("customer", "myattrbute2", array(
"type" => "varchar",
"backend" => "",
"label" => "My attribute-2",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute2");
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();
$installer->addAttribute("customer", "myattrbute3", array(
"type" => "varchar",
"backend" => "",
"label" => "My attribute-3",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute3");
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();
$installer->addAttribute("customer", "myattrbute4", array(
"type" => "varchar",
"backend" => "",
"label" => "My attribute-4",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute4");
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();
$installer->endSetup();
最后启用你的模块, 应用程序的/ etc /模块/ Packagename_Modulename.xml
<?xml version="1.0"?>
<config>
<modules>
<Packagename_Modulename>
<active>true</active>
<codePool>local</codePool>
<version>0.0.0</version>
</Packagename_Modulename>
</modules>
</config>
答案 1 :(得分:0)
我认为前面提到的代码中存在错误:
if(!$installer->attributeExists($entity, 'attribute_2')) {
$installer->removeAttribute($entity, 'attribute_2');
}
...似乎在说'如果新属性不存在 - 删除它&#39;
你可能想要这个:
if ($installer->attributeExists($entity, 'attribute_2')) {
$installer->removeAttribute($entity, 'attribute_2');
}