Now we know how to install EAV tables in our install scripts. Let’s add some attributes to those scripts, shall we?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute(‘modulename_content’, ‘title’, array(
‘type’ => ‘varchar’,
‘label’ => ‘Title’,
‘input’ => ‘text’,
‘class’ => “,
‘backend’ => “,
‘frontend’ => “,
‘source’ => “,
‘required’ => true,
‘user_defined’ => true,
‘default’ => “,
‘unique’ => false,
));
$installer->endSetup();
|
So, basically, all the action happens at the method addAttribute() . It accepts 3 arguments. The eav type, (defined inside of eav_entity_type
, the name of the attribute, and an array of options.
Let’s go through those options.
Type
This is the EAV type. It pertains to the tables you created. Types are int, decimal, datetime, text, and varchar.
Label
This is the label you create for your attribute. The “display name” if you will. This is the attribute name displayed to the user.
Input
This is the form type you use for this attributes’ input. This is the standard use of forms
Class
This is the class appended to the attribute when it is displayed. Good for styling as well as 3rd party features that use class identifiers on the html display.
Backend
The backend model is responsible for transformation of the content into the appropriate format necessary for insertion into the database, (as well as some other things). You can find backend models in app/code/core/Mage/Eav/Model/Entity/Attribute/Backend/.
Frontend
The exact opposite of backend. It is responsible for taking the data and transforming it into something that is user friendly or store specific. For example, the frontend model would take serialized data and unserialize it.
Source
The source is the source model responsible for populating default data into the attribute. A good example is a dropdown of US states for an address. These aren’t user defined, and are pretty much a constant, so this can be sent over via a source model for population.
Required
Pretty simple. This is a flag to see whethher or not this attribute is required when saving an entity. True or False.
User Defined
This defines whether or not this a user defined field. This flag will determine whether or not you can perform delete operations on this particular attribute from a UI perspective. Basically, if your attribute has an admin menu, this flag will determine whether or not users can delete this attribute.
Default
This is the default value for this particular attribute. If no options are defined by the user, this default value you specify will be entered.
Unique
Whether or not this value is unique among other attributes for this entity. If set to yes, no two attributes for this entity can be the same.
Overview : Magento Installation Scripts (Cool Ryan)
Previous : Part 4: EAV Tables
Next :Part 6: Data Installation and Upgrades
Post from coolryan.com