`
coolsooner
  • 浏览: 1311033 次
文章分类
社区版块
存档分类
最新评论

magento---EAV模型----总结!!

 
阅读更多

数据库添加eav模型

教程地址

http://magentobbs.com/?q=content/%E6%B7%BB%E5%8A%A0%E5%AE%9E%E4%BD%93%E7%B1%

BB%E5%9E%8B

eav模型原理:

产品表----(entity_type_id)---->属性表-----(attribute_id)----->值表

在上面这个关系中,

安装数据库

1

设置EntityType,主要是设置一些在安装过程需要用到的值。

$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 above "1" if we're using

eav/entity_increment_numeric

'increment_per_store' =>'0'

));

设置type----->设置model,table,为表的创建,model的读取提供值!!

总之,作为一个独立个体,设置值,为了得到在安装数据库过程中要用到的数据。

2

创建表

$this->getTable应该就是1过程中对table的值的设置的读取。

$installer->createEntityTables(

$this->getTable('helloworld-eav/blogpost')

);

创建表:

eavblog_posts

eavblog_posts_datetime

eavblog_posts_decimal

eavblog_posts_int

eavblog_posts_text

eavblog_posts_varchar

同时

eav_attribute_set多了一条数据。

在这个过程中,创建了主表和属性类型表,应该是这样

3

设置属性。

在这个过程中创建属性表

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',

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

'required' => true,

'user_defined' => true,

'default' => '',

'searchable' => false,

'filterable' => false,

'comparable' => false,

'visible_on_front' => false,

'unique' => false,

在此过程中,helloworld_eavblogpost为type值,也就是在在资源模型对应的类中使用

setType()函数中的参数,当然也要和sql/helloword-eav_setip/mysql4-install-

0.1.0.php 中addEntityType里面的值对应,在这里,table,attribute都要设置,值和

addEntityType的保持一致,不同的是多了一个attribute的值,

'attributes' => array(

'title' => array(

//the EAV attribute type, NOT a mysql varchar

'type' => 'varchar',

这里的表的内容,相当于正常表中的列!!

使用eav模型:

步骤:

1

编写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

资源--读写适配器

<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

设置相应的资源模型类

所以我们创建相应的资源模型类

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

记得添加模型集合
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

填写数据。

$weblog2 = Mage::getModel('helloworld-eav/eavblogpost');

$weblog2->setTitle('This is a test '.$i);

$weblog2->save();

7

读取数据

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 '<h1>'.$entry->getTitle().'</h1>';

}

echo '<br>Done<br>';

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics