magento—EAV模型—-總結!!

來源:互聯網
上載者:User

 

資料庫添加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>';

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.