在表单中添加自定义属性

时间:2011-04-05 10:39:07

标签: php magento entity-attribute-value

我使用的是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;
}

}

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

此致 SAM

好的我已经为有一个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
                ),

            ),
        )
    );
   }
}

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

你们可以在这帮忙吗? 感谢

3 个答案:

答案 0 :(得分:1)

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

<强> 1。将属性添加到数据库中的实体。

答。上面的安装脚本有效。使用Setup.php&amp; mysql_install / mysql_upgrade脚本。

<强> 2。属性应该可以从前端

进行编辑

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

第3。属性必须可以从后端

进行编辑

答。为此,我们需要在config.xml

中添加此代码段
<global>
        <fieldsets>
            <customer_account>
                <create>1</create><update>1</update>
           </customer_account>
       </fieldsets>
</global>

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

答案 1 :(得分:0)

这对我有用,请注意其他内容:

<?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();

答案 2 :(得分:-1)

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

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

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

相关问题