Magento News

 

Adding Custom attribute in forms

Hi There,

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 attributes 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'             => 'Pincode',
                    '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

Adding Custom attribute in forms

Possibly Related Posts:


 

Leave a Reply