Add an EAV model to a database
Tutorial address
Http://magentobbs.com /? Q = content/% E6 % B7 % BB % E5 % 8A % A0 % E5 % AE % 9e % E4 % BD % 93% E7 % B1 %
Bb % E5 % 9e % 8b
Principles of the EAV model:
Product table ---- (entity_type_id) ----> Attribute Table ----- (attribute_id) -----> value table
In the above relationship,
Install Database
1
Set entitytype mainly to set values required during the installation process.
$ Installer-> addentitytype ('helloworld _ eavblogpost', array (
// Entity_mode is the URL you 'd pass into a Mage: GetModel () call
'Entity _ model' => 'helloworld-EAV/eavblogpost ',
// Blank for now
'Attribute _ model' => '',
// Table refers to the resource URI helloworld-EAV/blogpost
// <Helloworld-eav_mysql4>... <Blogpost> <Table> eavblog_posts </table>
'Table' => 'helloworld-EAV/blogpost ',
// Blank for now, but can also be EAV/entity_increment_numeric
'Increment _ model' => '',
// Appears that this needs to be/can be abve "1" if we're using
EAV/entity_increment_numeric
'Crement _ per_store '=> '0'
));
Set Type -----> set model and table to create a table. The read value of the model is provided !!
In short, as an independent individual, set the value to obtain the data to be used during database installation.
2
Create a table
$ This-> gettable should be the read of the Table value setting in the 1 process.
$ Installer-> createentitytables (
$ This-> gettable ('helloworld-EAV/blogpost ')
);
Create a table:
Eavblog_posts
Eavblog_posts_datetime
Eavblog_posts_decimal
Eavblog_posts_int
Eavblog_posts_text
Eavblog_posts_varchar
At the same time
Eav_attribute_set has one more data entry.
In this process, the master table and attribute type table are created.
3
Set Properties.
Create an Attribute Table in this process
Class zhlmmc_helloworld_model_setup_entity_setup extends
Mage_eav_model_entity_setup {
Public Function getdefaultentities ()
{Return array (
'Helloworld _ eavblogpost' => array (
'Entity _ model' => 'helloworld-EAV/eavblogpost ',
'Attribute _ model' => '',
'Table' => 'helloworld-EAV/blogpost ',
'Bubuckets' => array (
'Title' => array (
// The EAV attribute type, not a MySQL varchar
'Type' => 'varchar ',
'Backend' => '',
'Frontend' => '',
'Label' => 'title ',
'Input' => 'text ',
'Class' => '',
'Source' => '',
// Store scope = 0
// Global scope = 1
// Website scope = 2
'Global' => 0,
'Visible '=> true,
'Requestred' => true,
'User _ defined' => true,
'Default' => '',
'Searchable' => false,
'Filterable' => false,
'Companyable' => false,
'Visible _ on_front '=> false,
'Unique' => false,
In this process, helloworld_eavblogpost is the type value, that is, it is used in the class corresponding to the resource model.
Parameters in the settype () function, of course also need to be with SQL/helloword-eav_setip/mysql4-install-
In 0.1.0.php, the value in addentitytype corresponds. Here, table and attribute must be set.
The addentitytype must be consistent. The difference is that an attribute value is added,
'Bubuckets' => array (
'Title' => array (
// The EAV attribute type, not a MySQL varchar
'Type' => 'varchar ',
The table content here is equivalent to the column in the normal table !!
Use the EAV model:
Steps:
1
Compile Model
2
Resourcemodel
<Helloworld-eav_mysql4>
<Class> zhlmmc_helloworld_model_resource_eav_mysql4 </class>
<Entities>
<Blogpost>
<Table> eavblog_posts </table>
</Blogpost>
</Entities>
Helloworld-eav_mysql4>
3
Resource-read/write Adapter
<Resources>
<! -... ->
<Helloworld-eav_write>
<Connection>
<Use> default_write </use>
</Connection>
Helloworld-eav_write>
<Helloworld-eav_read>
<Connection>
<Use> default_read </use>
</Connection>
Helloworld-eav_read>
</Resources>
4
Set the corresponding resource model class
Therefore, we create the corresponding resource model class.
File: APP/code/local/zhlmmc/helloworld/model/resource/EAV/mysql4/blogpost. php
Class zhlmmc_helloworld_model_resource_eav_mysql4_blogpost extends
Mage_eav_model_entity_abstract
{
Public Function _ construct ()
{
$ Resource = Mage: getsingleton ('Core/resource ');
$ This-> settype ('helloworld _ eavblogpost ');
$ This-> setconnection (
$ Resource-> getconnection ('helloworld-eav_read '),
$ Resource-> getconnection ('helloworld-eav_write ')
);
}
}
5
Remember to add a model set
Class zhlmmc_helloworld_model_resource_eav_mysql4_blogpost_collection extends mage_eav_model_entity_collection_abstract
{
Protected function _ construct ()
{
$ This-> _ Init ('helloworld-EAV/eavblogpost', 'helloworld-EAV/blogpost ');
}
}
6
Enter data.
$ Weblog2 = Mage: GetModel ('helloworld-EAV/eavblogpost ');
$ Weblog2-> settitle ('this is a test'. $ I );
$ Weblog2-> Save ();
7
Read data
Public Function eavshowcollectionaction (){
$ Weblog2 = Mage: GetModel ('helloworld-EAV/eavblogpost ');
$ Entries = $ weblog2-> getcollection ()-> addattributetoselect ('title ');
$ Entries-> load ();
Foreach ($ entries as $ entry)
{
// Var_dump ($ entry-> getdata ());
Echo '
}
Echo '<br> done <br> ';
}