开发者

Adding Custom attribute in forms

开发者 https://www.devze.com 2023-02-22 17:29 出处:网络
I am using magento V1.5. I am working on Customer EAV & have tried to create another EAV module. I have added few attributes in customer entity. Now MY requirement is those attributes must be edi

I am using magento V1.5. I am working on Customer EAV & have tried to create another EAV module.

I have added few attributes in customer entity. Now MY requirement is those attributes must be editable from frontend as well as backend.

Please tell me how to add those attributes in forntend form (customer edit form). & Tell me what to do in backend to have those options to be editable. I am thinking if there is a way in admin like in our Form.php we prepare form with adding elements to it. then we not need to write a code to create actual html. Magento automatically does it. SO idea is it must also load the new a开发者_StackOverflowttributes in that was just added. (like they appear in product edit.)

Second Issue is, Can u guys tell me what should I write in my Grid.php >> prepareCollection (for other EAV module ). so that it must get all the attributes with their values ( or may be few )

here is something that I have in my Grid.php but its not working

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

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

& this is my collection file

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

But its not returning anything in my grid

& here is my Attribute collection file

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;
}

}

Guys thank you so much. This forum has been specially very helpful to me

Regards SAM

Ok guys I have created a separate module for customers that has one mysql4_install file & it goes as follows

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

But when I hit the url for this module I see no error but these attributes are not in my database.

I also have created Entity_Setup file & it goes as follows I think its incorrect but I gave it a try

<?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
                ),

            ),
        )
    );
   }
}

But I see nothing, not any new attribute in database.

Can u guys help here whats wrong?? Thanks


Guys here is My solution. the above answers worked for adding attribute to database only My question has three parts.

1. Adding attributes to entity in database.

Ans. the above install scripts worked. using a Setup.php & mysql_install/ mysql_upgrade script.

2. Attribute should be editable from Frontend

Ans. We need to modify the file app/design/fontend/default//template/customer/form/edit.phtml

3. Attribute must be editable from Backend

Ans. for that we need to add this snippet in our config.xml

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

this will do everything Hope this will help somebody


This worked for me, notice the additional lines:

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


               )
           )

      );
    }
}

Install script is simply:

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


Having same issue here. Using addAttribute in 1.5.0.1 seems to add it into database but customer admin won't render it.

This method works fine with product and category attributes. And in 1.4.0.0 it works for customers too.

In Magento chagelog there is bullet point under improvements about customer attribute rendering. I'll start checking it next.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号