文章彙整

在Magento 1 新增Eav模組的資料欄位

Astral WebBy Astral Web 11 months agoNo Comments
首頁  /  Magento  /  在Magento 1 新增Eav模組的資料欄位

平常在使用資料欄位時,如果有多商店(multi-stores)的架構會變得非常複雜,也不易維護,但在Magento的產品管理是可以使用Eav模組可以完美解決這個問題。請參考以下範例:

  1. 先新增 etc module
    app/etc/modules/Astralweb_Test.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Astralweb_Test>
                <active>true</active>
                <codePool>local</codePool>
            </Astralweb_Test>
        </modules>
    </config>
    
    

     

  2. 新增 config.xml
    app/code/local/Astralweb/Test/etc/config.xml

    <?xml version="1.0"?>
    <config>
      <modules>
        <Astralweb_Test>
          <version>0.0.1</version>
        </Astralweb_Test>
      </modules>
      <global>
        <models>
          <astralweb_test>
            <class>Astralweb_Test_Model</class>
            <resourceModel>astralweb_test_resource</resourceModel>
          </astralweb_test>
          <astralweb_test_resource>
            <class>Astralweb_Test_Model_Resource</class>
            <entities>
              <page>
                <table>astralweb_test_page_entity</table>
              </page>
            </entities>
          </astralweb_test_resource>
        </models>
        <resources>
          <astralweb_test_setup>
            <setup>
              <module>Astralweb_Test</module>
              <class>Astralweb_Test_Model_Resource_Setup</class>
            </setup>
            <connection>
              <use>core_setup</use>
            </connection>
          </astralweb_test_setup>
          <page_write>
            <connection>
              <use>core_write</use>
            </connection>
          </page_write>
          <page_read>
            <connection>
              <use>core_read</use>
            </connection>
          </page_read>
        </resources>
      </global>
    </config>

     

  3. 新增setup
    app/code/local/Astralweb/Test/sql/astralweb_test_setup/install-0.0.1.php

    <?php
    
    $installer = $this;
    $installer->startSetup();
    
    /*
     * Create all entity tables
     */
    $installer->createEntityTables(
        $this->getTable('astralweb_test/page')
    );
    /*
     * Add Entity type
     */
    $installer->addEntityType(Astralweb_Test_Model_Resource_Page::ENTITY,Array(
        'entity_model'          =>'astralweb_test/page',
        'attribute_model'       =>'',
        'table'                 =>'astralweb_test/page',
        'increment_model'       =>'',
        'increment_per_store'   =>'0'
    ));
    
    $installer->installEntities();
    
    $installer->endSetup();

     

  4. 新增Model
    app/code/local/Astralweb/Test/Model/Page.php

    <?php
    class Astralweb_Test_Model_Page extends Mage_Core_Model_Abstract
    {
        protected function _construct()
        {
            $this->_init('astralweb_test/page','entity_id');
        }
    }

     

  5. 指定你的Model與資列表的名稱,新增預設的欄位
    app/code/local/Astralweb/Test/Model/Resource/Setup.php

    <?php
    class Astralweb_Test_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup
    {
        public function getDefaultEntities()
        {
            $entities = array(
                Astralweb_Test_Model_Resource_Page::ENTITY => array(
                    'entity_model' => 'astralweb_test/page',
                    'attribute_model' => '',
                    'table' => 'astralweb_test/page',
                    'attributes' => array(
                        'name' => array(
                            'type' => 'varchar',
                            'backend' => '',
                            'frontend' => '',
                            'label' => 'Name',
                            'input' => 'text',
                            'class' => '',
                            'source' => '',
                            'global' => 0,
                            'visible' => true,
                            'required' => true,
                            'user_defined' => true,
                            'default' => '',
                            'searchable' => false,
                            'filterable' => false,
                            'comparable' => false,
                            'visible_on_front' => true,
                            'unique' => false,
                        )
                    ),
                )
            );
            return $entities;
        }
    }

     

  6. 指定你的eav_entity_code的名稱,細節可以查看eav_entity_type這張表
    app/code/local/Astralweb/Test/Model/Resource/Page.php

    <?php
    class Astralweb_Test_Model_Resource_Page extends Mage_Eav_Model_Entity_Abstract
    {
       const ENTITY = "astralweb_test_page";
        /**
         * Resource initialization
         */
        public function __construct()
        {
            $resource = Mage::getSingleton('core/resource');
            $this->setType(self::ENTITY);
            $this->setConnection(
                $resource->getConnection('page_read'),
                $resource->getConnection('page_write')
            );
        }
    
        /*
         * Set default attributes
         */
        protected function _getDefaultAttributes()
        {
          return array(
              'entity_type_id',
              'attribute_set_id',
              'created_at',
              'increment_id',
              'updated_at',
              'store_id',
              'website_id'
          );
        }
    }

完成上述步驟後,接下來讓我們新增一個Controller來試試看我們的EAV模組。

首先,新增Collection讓我們可以抓取全部的資料

新增 Collection

app/code/local/Astralweb/Test/Model/Resource/Page/Collection.php

<?php
class Astralweb_Test_Model_Resource_Page_Collection extends Mage_Eav_Model_Entity_Collection_Abstract
{
    /*
     * Set resource model
     */
    protected function _construct()
    {
        $this->_init('astralweb_test/page');
    }
}

接著加入 Controller config

<config>
  ……...(之前的config)
  <frontend>
    <routers>
      <astral_test>
        <use>standard</use>
        <args>
          <module>Astralweb_Test</module>
          <frontName>astral_test</frontName>
        </args>
      </astral_test>
    </routers>
  </frontend>
</config>

app/code/local/Astralweb/Test/controllers/IndexController.php

<?php
class Astralweb_Test_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        //加入測試資料
        $page = Mage::getModel('astralweb_test/page');
        $page->setName("astralweb");
        $page->save();
        echo "save<br />";
        //抓取資料
        $pages = $page->getCollection()->addAttributeToSelect('*')->load();
        foreach ($pages as $row){
          echo $row->getName()."<br />";
        }
    }
}

測試成功後,就大功告成啦!

EAV在擴充上非常容易,並且支援多商店架構,Magneto原生就有提供支援。你也可以產生自己得eav_attribute_group 與 eav_attribute_set,這樣在後台上配置會非常的方便也更輕鬆,比起一張上百欄位的表,EAV更容易維護。

更多教學,請務必訂閱訂閱歐斯瑞電子報,以及追蹤我們的臉書粉絲團Instagram喔!

以上內容由Astralweb 歐斯瑞編寫製作

 000

推薦文章

Categories:
  MagentoMagento開發

留下回應

你的電子郵件地址不會被公開.

請到您的信箱確認,即可完成訂閱。