【问题标题】:Adding Custom attribute in forms在表单中添加自定义属性
【发布时间】:2011-04-05 10:39:07
【问题描述】:

我正在使用 magento V1.5。我正在开发客户 EAV 并尝试创建另一个 EAV 模块。

我在客户实体中添加了一些属性。现在我的要求是这些属性必须可以从前端和后端编辑。

请告诉我如何在 forntend 表单(客户编辑表单)中添加这些属性。 & 告诉我在后端做什么才能使这些选项可编辑。 我在想,如果在我们的 Form.php 中有一种管理方式,我们可以通过向其中添加元素来准备表单。那么我们不需要编写代码来创建实际的 html。 Magento 会自动执行此操作。所以想法是它还必须加载刚刚添加的新属性。 (就像它们出现在产品编辑中一样。)

第二个问题是,你们能告诉我应该在我的 Grid.php >> prepareCollection 中写什么(对于其他 EAV 模块)。因此它必须获取所有属性及其值(或者可能很少)

这是我的 Grid.php 中的一些东西,但它不起作用

protected function _prepareCollection()
{
    $collection = Mage::getModel('pincodes/eavpincodes')->getCollection();

    $this->setCollection($collection);
    return parent::_prepareCollection();
} 

&这是我的收藏文件

class Namespace_Pincodes_Model_Resource_Eav_Mysql4_Eavpincodes_Collection extends Mage_Eav_Model_Entity_Collection_Abstract
{
    protected function _construct()
    {
        $this->_init('pincodes/eavpincodes');
    }  
} 

但它没有在我的网格中返回任何东西

&这里是我的属性集合文件

class Inkfruit_Pincodes_Model_Resource_Eav_Mysql4_Attribute_Collection extends Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
{
public function _construct()
{
    $this->_init('pincodes/resource_eav_attribute', 'eav/entity_attribute');
}

protected function _initSelect()
{

    $this->getSelect()->from(array('main_table' => $this->getResource()->getMainTable()))
        ->where('main_table.entity_type_id=?', Mage::getModel('eav/entity')->setType('pincodes_eavpincodes')->getTypeId())
        ->join(
        array('additional_table' => $this->getTable('pincodes/eavpincodes')),
        'additional_table.attribute_set_id=main_table.attribute_id'  // I think this sql need to be changed but I have no idea what it'll be
        );
    return $this;
}

}

伙计们非常感谢你们。这个论坛对我特别有帮助

问候 山姆

好的,伙计们,我为具有一个 mysql4_install 文件的客户创建了一个单独的模块,其内容如下

$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('customer', 'profile_image', array(
                    'label'     => 'Profile Image',
                    'type'      => 'varchar',
                    'input'     => 'file',
                    'visible'   => true,
                    'required'  => false,
                    'user_defined' => true,
            ));

 $setup->addAttribute('customer', 'mobile', array(
                    'label'     => 'Mobile Number',
                    'type'      => 'int',
                    'input'     => 'text',
                    'visible'   => true,
                    'required'  => false,
                    'user_defined' => true,
            ));


$installer->endSetup();
$installer->installEntities();

但是当我点击这个模块的 url 时,我没有看到任何错误,但这些属性不在我的数据库中。

我还创建了 Entity_Setup 文件,它如下所示我认为它不正确,但我试了一下

<?php

class Namespace_Customer_Entity_Setup extends Mage_Eav_Model_Entity_Setup 
{
public function getDefaultEntities()
    {          
    return array (
        'customer' => array(
            'entity_model'      => 'customer/customer',
            'attribute_model'   => 'customer/attribute',
            'table'             => 'customer/entity',
            'attributes'        => array(
                'profile_image' => array(
                    //the EAV attribute type, NOT a mysql varchar
                    'type'              => 'varchar',
                    'backend'           => '',
                    'frontend'          => '',
                    'label'             => 'Profile Image',
                    'input'             => 'file',
                    'class'             => '',
                    'source'            => '',
                    // store scope == 0
                    // global scope == 1
                    // website scope == 2                           
                    'global'            => 0,
                    'visible'           => true,
                    'required'          => false,
                    'user_defined'      => true,
                    'default'           => '',
                    'searchable'        => true,
                    'filterable'        => true,
                    'comparable'        => false,
                    'visible_on_front'  => false,
                    'unique'            => false
                ),

                'mobile' => array(

                    'type'              => 'varchar',
                    'backend'           => '',
                    'frontend'          => '',
                    'label'             => 'Mobile Number',
                    'input'             => 'text',
                    'class'             => '',
                    'source'            => '',                          

                    'global'            => 0,
                    'visible'           => true,
                    'required'          => true,
                    'user_defined'      => true,
                    'default'           => '',
                    'searchable'        => true,
                    'filterable'        => true,
                    'comparable'        => false,
                    'visible_on_front'  => false,
                    'unique'            => false
                ),

            ),
        )
    );
   }
}

但我什么也没看到,数据库中没有任何新属性。

你们能帮忙看看有什么问题吗? 谢谢

【问题讨论】:

  • @Sasquiha 嘿,你能告诉我你在我的帖子中编辑了什么,它可能会有所帮助。
  • 没什么,只是加了php标签。

标签: php magento entity-attribute-value


【解决方案1】:

这里是我的解决方案。以上答案仅适用于向数据库添加属性 我的问题分为三个部分。

1.向数据库中的实体添加属性。

回答。上面的安装脚本有效。使用 Setup.php 和 mysql_install/mysql_upgrade 脚本。

2。属性应该可以从前端编辑

回答。我们需要修改文件app/design/fontend/default//template/customer/form/edit.phtml

3。属性必须可从后端编辑

回答。为此,我们需要在 config.xml 中添加这个 sn-p

<global>
        <fieldsets>
            <customer_account>
                <create>1</create><update>1</update>
           </customer_account>
       </fieldsets>
</global>

这将完成一切 希望这会对某人有所帮助

【讨论】:

    【解决方案2】:

    这对我有用,请注意附加行:

    <?php
    
    class Millena_CustomerExportAdditions_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
    {
        public function getDefaultEntities()
        {
            return array(
                'customer' => array(
                    'entity_model'          =>'customer/customer',
                    'attribute_model'       => 'customer/attribute',
                    'table'                 => 'customer/entity',
                    'additional_attribute_table' => 'customer/eav_attribute',
                    'entity_attribute_collection' => 'customer/attribute_collection',
                    'attributes' => array(
                        'export_status' => array(
                            //'group'             => 'Group/Tab',
                            'label'             => 'Customer Export Status',
                            'type'              => 'int',
                            'input'             => 'select',
                            'default'           => '0',
                            'class'             => '',
                            'backend'           => '',
                            'frontend'          => '',
                            'source'            => 'millena_customerExportAdditions/customer_attribute_source_exportStatus',
                            'global'            => 2,  //global scope
                            'visible'           => true,
                            'required'          => false,
                            'user_defined'      => false,
                            'searchable'        => false,
                            'filterable'        => false,
                            'comparable'        => false,
                            'visible_on_front'  => false,
                            'visible_in_advanced_search' => false,
                            'unique'            => false
                        )
    
    
                   )
               )
    
          );
        }
    }
    

    安装脚本很简单:

    <?php
    $installer = $this;
    $installer->installEntities();
    

    【讨论】:

      【解决方案3】:

      这里有同样的问题。在 1.5.0.1 中使用 addAttribute 似乎将其添加到数据库中,但客户管理员不会呈现它。

      此方法适用于产品和类别属性。在 1.4.0.0 中,它也适用于客户。

      在 Magento chagelog 中有关于客户属性呈现的改进的要点。接下来我会开始检查。

      【讨论】:

      • 实际上,我最近通过我的 mysql_install 文件为产品添加了一些属性,并猜测它们没有在管理区域中显示,无论是在管理属性中还是在产品编辑中。似乎我的代码出了点大问题:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多