数据库添加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>';

}

magento---EAV模型----总结!!相关推荐

  1. magento EAV 模型理解

    EAV模型是Zend框架的基础,而Magento项目又是建立在Zend框架的基础上的,所有了解EAV有助于了解Magento的架构原理,在开发Magento相关应用时非常有用. EAV : Entit ...

  2. Magento开发文档(七):Magento EAV模型

    在第一篇介绍Magento ORM的文章中,我们提到过Magento拥有两类模型.普通的模型及Entity Attribute Value(EAV)模型.这里首先搞清楚它们之前的一些关系. 所有的Ma ...

  3. Magento 2数据库EAV模型结构

     EAV模型是一种数据模型 ,用于描述实体的数量预计会很大,但事实上,实体中要使用的属性数量并不多. Magento 2这么设计是为了灵活性,在不影响主干的基础上,任意新增删除属性. EAV模型(E ...

  4. 【转】Magento 2数据库EAV模型结构

     EAV模型是一种数据模型 ,用于描述实体的数量预计会很大,但事实上,实体中要使用的属性数量并不多. Magento 2这么设计是为了灵活性,在不影响主干的基础上,任意新增删除属性. EAV模型(E ...

  5. java eav_动态自定义字段属性–Magento的EAV模型 | 学步园

    EAV : Entity - Attribute - Value 的缩写,是数据库模型的一种,使用eav建模的好处是可以动态为数据模型增加或移除属性. 1. 问题提出: 假设需要定义一个实体Custo ...

  6. Magento 数据表结构 EAV模型详解

    EAV : Entity - Attribute - Value的缩写,是数据库模型的一种,使用eav建模的好处是可以动态为数据模型增加或移除属性. 1.问题提出: 假设需要定义一个实体Custome ...

  7. Magento的EAV模型窥探

    EAV : Entity - Attribute - Value 的缩写,是数据库模型的一种,使用eav建模的好处是可以动态为数据模型增加或移除属性. [color=#BF0000][b]1. 问题提 ...

  8. mysql eav_Magento的EAV模型窥探

    1. 问题提出: 假设需要定义一个实体Customer的信息,通常我们只要定义一个表为customer,并定义相应的属性即可.倘若某天需要为customer增加一个新的属性如"毕业学校&qu ...

  9. java eav模式_Magento 2中EAV模型的理解

    今天我将要讲述Magento 2中的EAV模型,Magento 2的数据库使用的技术是一个 以实体-属性-值得模型的一种数据模型简称:EAV模型 实体-属性-值(Entity-Attribute-Va ...

  10. 使用 Eav 模型构建可无限扩展的数据存储能力

    举个例子 假设要做一个电商的商品管理,我们先卖一些衣服,需要管理衣服的尺码.颜色.款式等信息,有一天需要卖电脑了,电脑需要 主板.CPU.显卡.内存.硬盘.散热 等信息,过几天又需要卖手机了,手机有 ...

最新文章

  1. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
  2. HTML5之内嵌框架
  3. Windows事件等待学习笔记(一)—— 临界区自旋锁
  4. Glib 对 C 函数进行单元测试
  5. Android实现动画Dialog
  6. 介绍自己以及github注册流程
  7. 相乘的c语言,c语言矩阵相乘
  8. 学习vue3系列ref
  9. 单结晶体管的导电特性_二极管的导电特性
  10. C++ 哪些不能为虚函数
  11. 用“看板图”实现敏捷项目的可视化
  12. PyCharm:Error running xxx: Cannot run program D:\Python27\python.exe
  13. ActiveX控件注册的几种方法
  14. 查看linux有多少线程总数,linux线程总数
  15. 开关型直流电源比线性直流电源效率高的原因?
  16. LibPcap经常丢包怎么办?
  17. 为什么人工智能工程师被戏称为“调参侠”?
  18. 妹子:为什么我要找个程序员做老公?
  19. 水管工游戏---啊哈算法
  20. Linux 网络状态工具 ss 详解,比netstat更强大

热门文章

  1. 夜神模拟器:新建android模拟器并安装apk文件
  2. 统信 UOS 20 初体验
  3. linux open_namei,刚学Linux 想问问这个程序写的什么 有大佬帮帮吗
  4. 【待更新】GPU 保存模型参数,GPU 加载模型参数
  5. oracle数据库19C更改字体的大小及字体名称
  6. 测试需要掌握的数据库sql知识(一):基本语句操作
  7. 【中危】未加密的__VIEWSTATE参数
  8. Codeforces Beta Round #91 div.2 滑稽场 A-D
  9. 【Java】基础知识部分
  10. 笔试题算法系列(三)百度钓鱼比赛