数据库添加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-第六章 – 高级Magento模型 深入理解Magento-第七章 – 自定义Magento系统配置 深入理解Magento-第八章 – 深入自定义Magento系统配置(未完成) 深入理解Magento-第九章 – 修改、扩展、重写...
magento-ce-2.4.0_sample_data-2020-07-26-02-51-57.tar.gz Include sample data
magento-1.8.0.0.tar.gz
magento-ce-2.4.0-2020-07-24-11-15-38.tar.gz 不带sample data
Magento-Redismanager, 在 Magento,用于Redis的超级用户控制 Redis 管理Redis服务的实用程序特性系统> 配置>的自动或者手动配置高级> Redis管理在负载均衡环境中缓存服务不同的理想情况直接从magento面板管理员管理...
magento-1.6.2.0 magento-1.6.2.0 magento-1.6.2.0
magento -redis 安装包
因为国外网速太慢了,所以下载后分享给需要的人。magento-ce-2.4.2-p1-2021-04-22-10-49-09
最新的magento-1.7.0.2汉化包,汉化率达到98%上,绝对适合你
以详细的操作实例说明了magento的使用、配置、开发相关的实际应用。
magento-1.9.1.1-2015-04-30-12-49-08 源代码包
magento-演示数据1.6.1.0
在Magento 2站点内安装要在您的Magento 2项目中使用,可以使用: composer require --dev magento/magento-coding-standard 出于安全考虑,以这种方式安装时,无法自动添加phpcs的Magento标准。 您可以通过将以下...
Magento-CE-2.3.4_sample_data-2020-01-22-11-11-58.tar.gz源自官网
magento-enhancedemails-源码.rar
magento-1.6.1.0最新英文版
magento-1.7.0.7z,安装包
Ajax-Magento-ajax-add-to-cart.zip,[模块magento 1]magento ajax添加到购物车-ajoter vos produits au panier en ajax/感谢ajax将您的产品添加到购物车,ajax代表异步javascript和xml。它是多种web技术的集合,包括...
Magento-Vagrant-Puppet-Nginx 安装次数 Magento MySQL PHP PHP-FPM Nginx的 n98-玛格伦 设置 git submodule init git submodule update vagrant up 修改人偶/清单/base.pp class { "magento" : install_...
algoliasearch-magento-2, Algolia 2的搜索集成 Algolia搜索 Magento 2