2012-08-29 97 views
2

已经在我的头上敲了8个小时了...Magento。安装脚本不会运行,更不用说创建客户属性

我有需要(或更确切地说,我的公司确实是B2B)来请求额外的属性在客户注册。当然,Magento本身并没有这样做,所以我们被迫下降疯狂地令人费解构建路径使其如此。我买了一个模块来做到这一点,但发现代码是一个混乱(更不用说只是不工作)。

无论如何。我想我会推出自己的。

所以我找到了一个看起来很清楚并且重要的tutorial。所以我花了几分钟把它放在一起。没事了。

进一步谷歌福引导我herehere为额外的热智慧酱。


这里就是我有(运行Magento专业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> 



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> 



Setup.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安装-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’)); 

?> 

假设我理解正确,我已经掌握了一切......但没有什么,什么也没有。

值得注意的是:

  • 如果我删除设置类,我得到一个致命的错误,所以我觉得一切都到位。
  • 该模块显示在管理/系统/配置/高级/(启用);它被写入core_config_data
  • 我翻出所有的代码,只是使用空容器(调试的东西) - 安装脚本永远不会运行;它永远不会被写入core_resource

问题,然后是两个部分:

1.什么是防止我从运行安装脚本?
2.创建新客户属性的逻辑是否有声音?

假设它是 - 我想获得它到注册页面/客户账户/管理员帐户应该是相当简单...




...要去走了喝啤酒。

干杯。




更新


为@AlexeiYerofeyev想,customer名字本身就是问题。更改为CustomerAttr,脚本立即运行。

然后该模块运作按照期望的,但@ benmarks的解决方案似乎更清洁,让我重写匹配:

config.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> 

setup.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

我可以调用该字段中

主题/模板/客户/表格/ edit.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表与现有代码得到更新。




+4

信息上调试安装资源脚本是在这里:http://stackoverflow.com/questions/4717535/my-magento-extension-install-script-将不会运行 –

+0

感谢阿兰 - 我绝对看透了,但没有尝试过。顺便说一句 - 我去的时候让你的细节出来,类显然已经改写了变化,它不再是不清楚的地方你(1)调试将被放置。我没有花时间弄清楚 - 但想你想知道。 (也许社区/专业/企业之间的区别?)。 – Bosworth99

+0

你在布局上的电子书是天赐之物,顺便说一句。 – Bosworth99

回答

4

安装脚本有可能因其设置资源名称(customer_setup)而无法运行。由于核心扩展Mage_Customer具有设置资源的相同的名称,并已安装在core_resource表1.6.2.0版本或类似的东西,你有0.1.0版本的安装脚本可以被视为太旧,被忽略。所以你可以尝试将你的资源重新命名为独特的东西。

+2

不知道为什么你会陷入低谷。 'customer_setup'绝对是原因。 – ivantedja

+0

你在这里获得胜利 - 我改变了模块名称,我的脚本立即运行。欢呼 – Bosworth99

2

除非我失去了一些东西:

在你的安装脚本,安装这些属性,你应该只使用customer/setupMage_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(); 
+0

这绝对是一个更好的方法,尽管我原来的脚本也有功能。 – Bosworth99

+0

是的,但除非您在这些属性实例上调用'save()',否则我确信'used_in_forms'数据不会被写入。 – benmarks

+0

所以 - 这里总猜测 - 但我发现基本上有两种不同的方法来做到这一点:首先是你的方法,直接调用addAttribute(),和发送(我第一次使用),只是增加额外的值到默认的magento行为(并且可能本身调用addattribute())...无论如何,两个版本都可以工作。 (见我的更新)。 – Bosworth99

0

我不知道是否Magento的实际运行安装脚本正确,如果你在一段时间做不起来的版本号一次。

考虑弹出你的模块了一个版本,并使用升级脚本。在更新脚本中放置一个简单的Mage :: log,你会看到它是否真的被调用。

版本云在应用程序的/ etc /模块/ blah_Blah.xml和等/ config.xml中加有在core_Config说什么它认为它是对的条目。

也许只是在原始安装程序中设置表格,然后在升级脚本中插入数据内容。

艾伦的文章有所有你需要升级脚本:http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-6-magento-setup-resources

+0

几点:1)版本号不会被应用程序评估,直到每个活动模块的'config.xml'被合并后,所以它不需要在模块声明文件中,并且有点非常规2)操作EAV直接配置表可能比通过各自的类在定义的工作流中处理它们更容易出错。 – benmarks