打了 8 个小时了......
我需要(或者更确切地说,我的公司是 B2B(在客户注册时请求额外的属性。当然,Magento本身并不是这样做的,所以我们被迫走上了非常复杂的构建路径来做到这一点。我买了一个模块来做这件事,但发现代码一团糟(更不用说根本无法正常工作了(。
无论什么。我想我会自己滚
。所以我找到了一个看起来清晰而中肯的教程。所以我,花几分钟把它放在一起。和。。。。无。
进一步的谷歌富引导我在这里和这里寻找额外的辣智慧酱。
这是我所拥有的(运行Magento Professional v1.11(:
文件结构:
WACI
- Customer
-- etc
--- config.xml
-- Model
--- Resource
---- Eav
----- Mysql4
------ Setup.php
-- sql
--- customer_setup
---- mysql4-install-0.1.0.php
等/模块/WACI_All.xml
<config>
<modules>
<WACI_Customer>
<active>true</active>
<codePool>local</codePool>
</WACI_Customer>
</modules>
</config>
配置.xml
<config>
<modules>
<WACI_Customer>
<version>0.1.0</version>
</WACI_Customer>
</modules>
<global>
<fieldsets>
<customer_account>
<title><create>1</create><update>1</update></title>
<phone><create>1</create><update>1</update></phone>
<agency><create>1</create><update>1</update></agency>
<fed_id><create>1</create><update>1</update></fed_id>
<ubi><create>1</create><update>1</update></ubi>
</customer_account>
</fieldsets>
<!--<models>
<customer>
<class>WACI_Customer_Model</class>
</customer>
</models> -->
<resources>
<customer_setup>
<setup>
<module>WACI_Customer</module>
<class>WACI_Customer_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customer_setup>
<customer_write>
<connection>
<use>core_write</use>
</connection>
</customer_write>
<customer_read>
<connection>
<use>core_read</use>
</connection>
</customer_read>
</resources>
</global>
</config>
设置.php
<?php
class WACI_Customer_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_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,
'attribute_model' => 'customer/attribute',
'attributes' => array(
'title' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Title / Position',
'visible' => true,
'required' => false,
'position' => 63,
),
'phone' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Telephone',
'visible' => true,
'required' => true,
'position' => 64,
),
'agency' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Agency / Organization',
'visible' => true,
'required' => false,
'position' => 65,
),
'fed_id' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Fed ID',
'visible' => true,
'required' => false,
'position' => 66,
),
'ubi' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'UBI',
'visible' => true,
'required' => false,
'position' => 67,
),
),
),
);
}
}
?>
mysql4-install-0.1.0.php
<?php
Mage::log('Installing WACI_Customer');
// die ( echo 'Running This Upgrade: '.get_class($this)."'n <br /> 'n"; );
$installer = $this;
$installer->installEntities();
$eavConfig = Mage::getSingleton(‘eav/config’);
$attribute_title = $eavConfig->getAttribute(‘customer’, 'title');
$attribute_phone = $eavConfig->getAttribute(‘customer’, 'phone');
$attribute_agency = $eavConfig->getAttribute(‘customer’, 'agency');
$attribute_fedid = $eavConfig->getAttribute(‘customer’, 'fed_id');
$attribute_ubi = $eavConfig->getAttribute(‘customer’, 'ubi');
// put into customer_form_attribute table so field will show in admin.
$attribute_title->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_phone->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_agency->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_fedid->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_ubi->setData(‘used_in_forms’, array(‘adminhtml_customer’));
?>
假设我理解正确,我已准备好一切...但是什么都没有,什么都没有,更什么都没有。
特别是:
- 如果我删除安装程序类,我会收到一个致命错误,所以我认为一切都到位了。
- 该模块显示在 admin/system/configuration/advanced/(已启用(中;它被写入
core_config_data
- 我删除了所有代码,只使用了空容器(带有调试内容( - 安装脚本永远不会运行; 它永远不会被写入
core_resource
问题,那么是两部分:
1. 是什么阻止了我的安装脚本运行?
2. 创建新客户属性的逻辑是否合理?
假设它是 - 我认为将其放入注册页面/客户帐户/管理员帐户应该相当简单......
...现在去喝啤酒。
干杯。
更新
正如@AlexeiYerofeyev所想的那样,customer
名称本身就是问题所在。更改为 CustomerAttr,脚本立即运行。
然后该模块按预期运行,但@benmarks的解决方案似乎更干净,所以我重写了以匹配:
配置.xml 0.1.0
<global>
<resources>
<customerattr_setup>
<setup>
<module>WACI_CustomerAttr</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customerattr_setup>
</resources>
<fieldsets>
<customer_account>
<title><create>1</create><update>1</update></title>
<phone><create>1</create><update>1</update></phone>
<agency><create>1</create><update>1</update></agency>
<fed_id><create>1</create><update>1</update></fed_id>
<ubi><create>1</create><update>1</update></ubi>
</customer_account>
</fieldsets>
</global>
</config>
设置.php
<?php
Mage::log('Installing WACI_CustomerAttr');
echo 'Running Upgrade: '.get_class($this)."'n <br /> 'n";
//die ( 'its running' );
$installer = $this;
/* @var $installer Mage_Customer_Model_Entity_Setup */
$installer->startSetup();
$installer->addAttribute('customer','agency',
array(
'type' => 'varchar',
'label' => 'Agency / Organization',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 62,
)
);
$installer->addAttribute('customer','title',
array(
'type' => 'varchar',
'label' => 'Title / Position',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 63,
)
);
$installer->addAttribute('customer','phone',
array(
'type' => 'varchar',
'label' => 'Telephone',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 64,
)
);
$installer->addAttribute('customer','fed_id',
array(
'type' => 'varchar',
'label' => 'Fed ID',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 65,
)
);
$installer->addAttribute('customer','ubi',
array(
'type' => 'varchar',
'label' => 'UBI',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 66,
)
);
$attrs = array('agency','title','phone','fed_id','ubi');
foreach ($attrs as $item) {
$attr = Mage::getSingleton('eav/config')->getAttribute('customer', $item);
$attr->setIsUsedInForms(array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
}
$installer->endSetup();
?>
模块(和版本(成功写入core_resource
,属性成功添加到eav_attribute
。
我可以调用字段
主题/模板/客户/表单/编辑.phtml
<div class="input-box">
<label for="agency"><?php echo $this->__('Agency / Organization') ?><span class="required">*</span></label><br />
<input type="text" name="agency" id="agency" value="<?php echo $this->htmlEscape($this->getCustomer()->getAgency()) ?>" title="<?php echo $this->__('Agency') ?>" class="required-entry input-text" />
</div>
但是,不幸的是,该值没有写入customer_entity_varchar
(最终,我的初始脚本也是如此。
因此,我在表中获得了该属性,但尚未将其添加到客户实体中。
似乎一旦我有了这个工作,我应该能够随时随地随意读写该值。
关于我现在如何进行的任何想法?
最终更新
好的,解决了这个问题:
$attr->setData('used_in_forms', array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
而不是
$attr->setIsUsedInForms(array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
对于任何可能需要信息的人...
事实证明,数组没有使用先前的代码在customer_form_attribute
表中更新。
安装脚本有可能由于其安装资源名称 (customer_setup( 而无法运行。由于核心扩展Mage_Customer具有相同的安装资源名称,并且它已经安装在core_resource版本中的版本为 1.6.2.0 或类似版本,因此具有版本 0.1.0 的安装脚本可能会被视为太旧并被忽略。因此,您可以尝试将资源重命名为唯一名称。
除非我错过了什么:
在安装脚本中,要安装这些属性,您应该只使用 customer/setup
类Mage_Customer_Model_Resource_Setup
并使用每个属性配置调用 addAttribute()
,(不幸的是(遵循表单设置逻辑。
<?php
$installer = Mage::getResourceModel('customer/setup', 'default_setup');
/* @var $installer Mage_Customer_Model_Resource_Setup */
$installer->startSetup();
$installer->addAttribute(
'customer',
'title',
array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Title / Position',
'visible' => true,
'required' => false,
'position' => 63,
)
);
/**
the same for others, then individually or in loop:
*/
$attr = Mage::getSingleton('eav/config')->getAttribute('customer_address', 'title');
$attr->setIsUsedInForms(array('adminhtml_customer'))->save();
//NB: that save() call is fairly important ;-)
$installer->endSetup();
不偶尔增加版本号,我不知道Magento是否真的正确运行安装脚本。
考虑弹出模块版本并使用升级脚本。在更新脚本中输入一个简单的 Mage::log,您将看到它是否真的被调用。
该版本在应用程序/etc/模块/blah_Blah.xml 和 etc/config 中.xml此外,core_Config中还有一个条目,说明它认为它是什么。
也许只是在原始安装程序中设置表,然后在升级脚本中插入数据内容。
Alan的文章包含有关升级脚本的所有内容:http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-6-magento-setup-resources