深入理解Magento
作者:
Alan Storm
翻譯:
Hailong Zhang
第六章 – 高級Magento模型
我們講過Magento有兩種模型,簡單模型和EAV(Entity Attribute Value)模型。上一章我們講過所有的Magento模型都是繼承自Mage_Core_Model_Abstract / Varien_Object。簡單模型和EAV模型的區(qū)別在于資源模型(Model Resource)。雖然所有的資源模型都最終繼承“Mage_Core_Model_Resrouce_Abstract”,但是簡單模型是直接繼承 “Mage_Core_Model_Mysql4_Abstract”,而EAV模型是直接繼承 “Mage_Eav_Model_Entity_Abstract”。
Magento這么做是由它的道理的。對于大部分開發(fā)人員或者用戶來說,他們只需要知道一系列的方法能夠操作模型,獲得數(shù)據(jù),數(shù)據(jù)到底是如何存儲的并不是很重要。
?
什么是EAV模型?
Wikipedia是這么定義的:
???
EAV(Entity-Attribute-Value)模型,也作Object-Attribute-Value模型或者開放模型是一種數(shù)據(jù)模型。這種數(shù)據(jù)模型常常用在一個對象的屬性數(shù)目不是一定的情況下。在數(shù)學(xué)上,這種模型稱為松散矩陣。
?
換一種方式理解,EAV模型就是數(shù)據(jù)表的一種泛化。在傳統(tǒng)的數(shù)據(jù)庫中,數(shù)據(jù)表的列的數(shù)量是一定的
+——————+ | products | +——————+ | product_id | | name | | price | | etc.. | +——————+ +————+—————-+——————+———+ | product_id | name | price | etc… | +————+—————-+——————+———+ | 1 | Widget A | 11.34 | etc… | +————+—————-+——————+———+ | 2 | Dongle B | 6.34 | etc… | +————+—————-+——————+———+?
在上面這張表中,每一個商品都有名稱,價格等等。
在EAV模型中,每一個模型都有不同的屬性。這對于電子商務(wù)的應(yīng)用來說是很合適的。比如說一個網(wǎng)店可以賣筆記本,擁有CPU速度,顏色,內(nèi)存等屬性,但是 網(wǎng)店也可以賣衣服,有顏色屬性,但是沒有CPU速度。即使是賣衣服的網(wǎng)店,也有上衣和褲子之分,它們的屬性也是不一樣的。
?
有很多開源的或者商業(yè)的數(shù)據(jù)庫是默認(rèn)使用EAV模型的。但是一般的網(wǎng)站托管平臺不提供這些數(shù)據(jù)庫。所以Varien開發(fā)了一套基于PHP和MySQL的EAV系統(tǒng)。換句話說,它們在傳統(tǒng)的關(guān)系型數(shù)據(jù)庫上面開發(fā)了一套EAV數(shù)據(jù)庫系統(tǒng)。
在使用的時候,EAV模型的屬性是會分布在不同的MySQL數(shù)據(jù)表中。
?
上面的這張圖是Magento中關(guān)于“catalog_product”的表。每一個產(chǎn)品都是“catalog_product_entity”中的一 行。Magento系統(tǒng)中所有的屬性(不僅僅是商品)都存放在“eav_attribute”表中,而屬性的值都放在類似下面的表中 “catalog_product_entity_attribute_varchar”, “catalog_product_entity_attribute_decimal”, “catalog_product_entity_attribute_etc”。【譯者注:如果你仔細(xì)觀察上面這幅數(shù)據(jù)表結(jié)構(gòu)圖,你會發(fā)現(xiàn)明顯少了一張 表,和“entity_type”有關(guān)。因為這里有“entity_type_id”出現(xiàn),但卻沒有定義這個屬性的表。這個表在Magneto中叫做 “eav_entity_type”。由于EAV模型中所有的模型數(shù)據(jù)都混在一套數(shù)據(jù)表中了,實體類型(entity_type)就是用來把不同的模型區(qū) 別開來的屬性。假如我們要找出系統(tǒng)中所有的產(chǎn)品數(shù)據(jù),那么Magento先通過“eav_entity_type”表獲得產(chǎn)品模型的 “entity_type_id”,然后再通過上面這幅圖的關(guān)系來拿到所有的數(shù)據(jù)。
?
在EAV系統(tǒng)下面,當(dāng)你需要添加一個屬性的時候,只需要在“eav_attribute”表中添加一行就行了。而傳統(tǒng)的關(guān)系型數(shù)據(jù)庫則需要修改數(shù)據(jù)表調(diào)用 “ALTER TABLE”語句,復(fù)雜而且有風(fēng)險。EAV模型的缺點(diǎn)是你不能通過一個簡單的SQL語句就獲得一個模型的所有屬性。你往往需要調(diào)用多個SQL或者一個 SQL包干了多個join語句。
?
實戰(zhàn)EAV模型
我們已經(jīng)介紹了EAV是怎么工作的了。下面我們要通過一個例子來說明要在Magento中創(chuàng)建一個EAV模型所需要的步驟。這部分內(nèi)容大概是 Magento中最令人頭疼的部分,95%的Magento用戶都不會和這些代碼打交道,但是理解EAV模型的原理能夠幫助你更好的理解Magento的 代碼和架構(gòu)。
?
因為EAV模型的內(nèi)容太多了,所以我假設(shè)你已經(jīng)熟悉了前幾章的內(nèi)容,包括Magento MVC,組類名等等。在這一章我不會再重復(fù)這些內(nèi)容。
?
EAV形式的Hello World
我們將為Hello World模塊創(chuàng)建另外一個模型,使用EAV形式的資源模型。首先我們?yōu)槟K創(chuàng)建一個新的模型叫做“Eavblogpost”。記住,簡單模型和EAV模型的區(qū)別是資源模型,所以我們創(chuàng)建一個模型的基本步驟是一樣的。
<global> <!– … –> <models> <!– … –> <helloworld-eav> <class>Zhlmmc_Helloworld_Model</class> <resourceModel>helloworld-eav_mysql4</resourceModel> </helloworld-eav> <!– … –> </models> <!– … –> </global>?
我想我不說你也應(yīng)該知道,我們要創(chuàng)建一個新的模型文件。由于PHP 5.3和命名空間(namespaces)還沒有被廣泛采用,Magento中的類名仍然和文件的物理路徑相關(guān)。這就導(dǎo)致了很多時候不知道一個URI所對 應(yīng)的類究竟該放在什么文件夾下面。我發(fā)現(xiàn)我們可以利用Magento的異常信息來直接得到一個類的路徑。比如,這里我們先不創(chuàng)建模型類,先來修改 BlogController來直接使用模型類,這樣Magento就會報錯說找不到模型類,并給出路徑
public function eavReadAction(){ $eavModel = Mage::getModel('helloworld-eav/eavblogpost'); echo get_class($eavModel)."<br/>"; }?
清空Magento緩存,訪問以下URL
http://127.0.0.1/Magento/helloworld/blog/eavRead
跟預(yù)計的一樣,你應(yīng)該得到以下異常
Warning: include(Zhlmmc/Helloworld/Model/Eavblogpost.php) [function.include]: failed to open stream: No such file or directory?
所以我們應(yīng)該創(chuàng)建如下文件
File: app/code/local/Zhlmmc/Helloworld/Model/Eavblogpost.php
class Zhlmmc_Helloworld_Model_Eavblogpost extends Mage_Core_Model_Abstract { protected function _construct() { $this->_init('helloworld-eav/blogpost'); } }?
刷新頁面,你應(yīng)該看到下面的輸出
Zhlmmc_Helloworld_Model_Eavblogpost?
下面我們來創(chuàng)建資源模型。先定義資源模型
<helloworld-eav_mysql4> <class>Zhlmmc_Helloworld_Model_Resource_Eav_Mysql4</class> <entities> <blogpost> <table>eavblog_posts</table> </blogpost> </entities> </helloworld-eav_mysql4>?
這里的標(biāo)簽名字和我們上面定義的模型的<resourceMode />是一致的。<entities />的定義和上一章是一樣的。下面的適配器的定義
<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>?
然后再次利用Magento的異常,先修改“eavReadAction”
public function eavReadAction(){ $eavModel = Mage::getModel('helloworld-eav/eavblogpost'); $params = $this->getRequest()->getParams(); echo("Loading the blogpost with an ID of ".$params['id']."<br/>"); $eavModel->load($params['id']); $data = $eavModel->getData(); var_dump($data); }?
清空Magento緩存,訪問URL
http://127.0.0.1/Magento/helloworld/blog/eavRead/id/1
你應(yīng)該看到如下異常
Warning: include(Zhlmmc/Helloworld/Model/Resource/Eav/Mysql4/Blogpost.php) [function.include]: failed to open stream: No such file or directory?
所以我們創(chuàng)建相應(yīng)的資源模型類
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') ); } }?
這個類和簡單的資源模型就不一樣。首先,我們這里繼承的是“Mage_Eav_Model_Entity_Abstract”。其次,我們沒有調(diào)用 “_init”方法。在EAV模型中我們需要自己來完成資源模型初始化的過程,包括,告訴資源模型使用哪個適配器,以及實體類型 (entity_type)。刷新URL,你應(yīng)該看到如下異常
Invalid entity_type specified: helloworld_eavblogpost?
根據(jù)我們上文所講的內(nèi)容,那這個異常的原因很明顯,那就是“eav_entity_type”表中,沒有需要的 “helloworld_eavblogpost”的數(shù)據(jù)。這里的“helloworld_eavblogpost”就是我們“setType”的參數(shù)。 讓我們來看一下這張表長什么樣
mysql> select * from eav_entity_type\G *************************** 1. row *************************** entity_type_id: 1 entity_type_code: customer entity_model: customer/customer attribute_model: entity_table: customer/entity value_table_prefix: entity_id_field: is_data_sharing: 1 data_sharing_key: default default_attribute_set_id: 1 increment_model: eav/entity_increment_numeric increment_per_store: 0 increment_pad_length: 8 increment_pad_char: 0 additional_attribute_table: customer/eav_attribute entity_attribute_collection: customer/attribute_collection *************************** 2. row *************************** entity_type_id: 2 entity_type_code: customer_address entity_model: customer/customer_address attribute_model: entity_table: customer/address_entity value_table_prefix: entity_id_field: is_data_sharing: 1 data_sharing_key: default default_attribute_set_id: 2 increment_model: increment_per_store: 0 increment_pad_length: 8 increment_pad_char: 0 additional_attribute_table: customer/eav_attribute entity_attribute_collection: customer/attribute_collection?
正如我們前面講過的,這張表包含了所有系統(tǒng)中的實體類型。我們的參數(shù)“helloworld_eavblogpost”就是實體類型的值,對應(yīng)數(shù)據(jù)表列“entity_type_code”。
?
系統(tǒng)和應(yīng)用程序
這一章講的內(nèi)容是Magento最重要的一個概念,也是很多人覺得頭疼的概念。拿電腦來做比方。操作系統(tǒng),比如Mac OS X,Windows,Linux等等,是軟件系統(tǒng),而瀏覽器,比如FIrefox,Safari,IE等等是應(yīng)用程序。Magento首先是一個系統(tǒng),其 次才是一個應(yīng)用程序。你可以在Magento系統(tǒng)之上創(chuàng)建一個電子商務(wù)應(yīng)用。令人感到困惑的是Magento的代碼在很多地方是以很原始的方式暴露給應(yīng)用 程序的。EAV系統(tǒng)的配置和你網(wǎng)店的數(shù)據(jù)存放在統(tǒng)一數(shù)據(jù)庫中就是一個例子。
?
隨著你越來越深入Magento,你需要把Magento當(dāng)作老式的 IBM 650 機(jī)器。也就是說,你必須對Magento有很深的了解才能對它運(yùn)用自如。【譯者注:這一段和上下文沒什么關(guān)系,大概是作者有感而發(fā)】
?
創(chuàng)建資源配置
從理論上講,你可以手動的在數(shù)據(jù)庫中插入數(shù)據(jù),讓我們的EAV模型工作,但我還是不建議你這么做。所幸的是,Magento提供了一個特殊的資源配置類,包含了一些有用的方法能自動的創(chuàng)建一些數(shù)據(jù),使得系統(tǒng)能工作。
?
我們先添加資源配置
<resources> <!– … –> <helloworld-eav_setup> <setup> <module>Zhlmmc_Helloworld</module> <class>Zhlmmc_Helloworld_Model_Entity_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </helloworld-eav_setup> <!– … –> </resources>?
創(chuàng)建資源配置類文件
File: app/code/local/Zhlmmc/Helloworld/Model/Entity/Setup.php
class Zhlmmc_Helloworld_Model_Entity_Setup extends Mage_Eav_Model_Entity_Setup { }?
請注意,這里我們繼承的父類是“Mage_Eav_Model_Entity_Setup”。最后,我們來創(chuàng)建安裝腳本。如果你不熟悉這部分內(nèi)容,請你參考前面章節(jié)的內(nèi)容。
File: app/code/local/Zhlmmc/Helloworld/sql/helloworld-eav_setup/mysql4-install-0.1.0.php
<?php $installer = $this; throw new Exception("This is an exception to stop the installer from completing"); ?>?
清空Magento緩存,訪問任何頁面,你應(yīng)該看到以上異常。如果你沒有看到異常,那說明你哪里配置錯了。
請注意:我們將一步一步的創(chuàng)建安裝腳本。如果你閱讀了前面的章節(jié),你應(yīng)該知道我們必須刪除“core_resource”數(shù)據(jù)表中的相應(yīng)數(shù)據(jù)才能使得安裝 腳本重新運(yùn)行。所以在我們下面的例子中,當(dāng)我們修改了安裝腳本,我們都默認(rèn)會刪除“core_resource”表中的數(shù)據(jù)。正常使用Magento的時 候我們不需要這樣做的,教程中的例子是極端情況。
?
添加實體類型
首先我們修改安裝腳本如下
$installer = $this; $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' ));?
我們調(diào)用了資源配置對象的“addEntityType”方法。這個方法的參數(shù)是實體類型(helloworld_eavblogpost)還有和這個類 型相關(guān)的參數(shù)。當(dāng)你運(yùn)行這個腳本以后,你會發(fā)現(xiàn)“eav_attribute_group”,“eav_attributeset”還有 “eav_entity_type”數(shù)據(jù)表中有了新的數(shù)據(jù)。訪問以下URL
http://127.0.0.1/Magento/helloworld/blog/eavRead/id/1
你應(yīng)該看到以下異常
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'zend-magento.eavblog_posts' doesn't exist?
創(chuàng)建數(shù)據(jù)表
我們已經(jīng)告訴Magento我們的實體類型。接下來,我們要創(chuàng)建用來存儲數(shù)據(jù)的數(shù)據(jù)表,并配置系統(tǒng)讓Magento知道我們要用這些表。
?
如果你研究過Magento核心模塊的資源配置腳本的話,比如core/Mage/CatalogInventory的配置腳本,你會看到很多用來創(chuàng)建數(shù) 據(jù)表的SQL語句。所幸的是,我們已經(jīng)不必要這樣做了。Magento提供的資源配置類有一個方法“createEntityTables”。我們可以用 這個方法來創(chuàng)建我們需要的數(shù)據(jù)表。同時這個方法也會在Magento的系統(tǒng)數(shù)據(jù)表中添加相應(yīng)的配置數(shù)據(jù)。
$installer->createEntityTables( $this->getTable('helloworld-eav/blogpost') );?
“createEntityTables”有兩個參數(shù)。第一個參數(shù)是基礎(chǔ)表名(base table name)。第二個參數(shù)是一系列選項。我們這里忽略了第二個參數(shù),這些參數(shù)都是一些高級配置,超出了我們討論的范圍。在運(yùn)行了上述腳本以后,你會發(fā)現(xiàn)數(shù)據(jù) 庫中添加了如下數(shù)據(jù)表
eavblog_posts
eavblog_posts_datetime
eavblog_posts_decimal
eavblog_posts_int
eavblog_posts_text
eavblog_posts_varchar
同時,你會發(fā)現(xiàn)在“eav_attribute_set”表中多了一條數(shù)據(jù)
mysql> select * from eav_attribute_set order by attribute_set_id DESC LIMIT 1 \G *************************** 1. row *************************** attribute_set_id: 63 entity_type_id: 31 attribute_set_name: Default sort_order: 3?
清空Magento緩存,重新訪問如下URL
http://127.0.0.1/Magento/helloworld/blog/eavRead/id/1
你應(yīng)該看到以下輸出
Loading the blogpost with an ID of 1 array(0) { }?
添加屬性
創(chuàng)建資源配置的最后一步是告訴Magento我們的模型有哪些屬性。這就和為單獨(dú)的數(shù)據(jù)表添加列是一樣的。【譯者注:我們上面的輸出是空的就是因為 我們雖然創(chuàng)建了EAV數(shù)據(jù)表,但是卻沒有創(chuàng)建EAV屬性,就像創(chuàng)建了一張沒有任何列的數(shù)據(jù)表,當(dāng)然是空的。】和上面的步驟一樣,Magento的資源配置 類提供了相應(yīng)的幫助函數(shù),“installEntities”和“getDefaultEntities”。
?
我們之前所做的是告訴Magento,我們創(chuàng)建了一個實體類型(Entity Type),而現(xiàn)在,我們要配置這個實體類型使它能夠和我們的模型相符合。這個方法名字有點(diǎn)搞“installEntities”,其實我們要做的是配置 這個實體。修改類“Zhlmmc_Helloworld_Model_Setup_Entity_Setup”
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, ), ), ) ); } }?
這里我們構(gòu)建了一個數(shù)組,數(shù)組的元素是“key/value”對。“key”就是實體類型的名字(以下代碼參數(shù)是一樣的 “$installer->addEntityType('helloworld_eavblogpost',…)”),“value”是一個數(shù) 組,用來描述這個實體類型。“value”數(shù)組的元素大部分你應(yīng)該都見過,就不多解釋了。這里要關(guān)注的是“attribute”元素,這個元素的值又是一 個數(shù)組。這個數(shù)組的內(nèi)容就是我們定義的實體類型的屬性,相當(dāng)于普通數(shù)據(jù)表的列,比如這里的“title”。很可惜,我無法完整解釋用來描述一個屬性的數(shù)組 的內(nèi)容。在這里,我們只要知道“type”就是這個屬性的數(shù)據(jù)類型“varchar”。也就是說,這個屬性的值將會被保存到 “eavblog_posts_varchar”數(shù)據(jù)表中。其他的很多元素都是和Magento的后臺管理有關(guān)。Magento很多地方的UI是由模型控 制的,很多這些參數(shù)都是用來控制UI顯示和系統(tǒng)設(shè)置。這樣做的優(yōu)點(diǎn)是靈活性提高,但是缺點(diǎn)是這些內(nèi)容對于外部開發(fā)者都是不透明的。【譯者注:我們是可以在 這個函數(shù)中返回多個實體類型的。如果返回多個實體類型,那就說明模塊擁有多個模型。】
?
順便說一下,Magento選擇使用數(shù)組嵌套數(shù)組的形式來表示實體類型的屬性很奇怪。因為Magento整個架構(gòu)是非常面向?qū)ο蟮摹_@里的數(shù)據(jù)結(jié)構(gòu)和系統(tǒng)的其他部分很不一樣。
?
接下來我們需要修改安裝腳本,添加如下代碼
$installer->installEntities();?
“installEntities”會調(diào)用“getDefaultEntities”方法來獲取將要被配置的屬性。當(dāng)然你也可以把屬性直接作為參數(shù)傳給 “installEntities”,但是我覺得還是按照Magento的習(xí)慣來比較好。在調(diào)用“installEntitis”以后,Magento會 做下面兩件事
- 在“eav_attribute”表中添加“title”屬性
- 在“eav_entity_attribute”表中添加一行
清空Magento緩存,刷新頁面,你應(yīng)該看到如下異常
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails?
那是因為我們之前已經(jīng)調(diào)用過一次“createEntityTables”,再次調(diào)用的時候Magento會嘗試先刪除數(shù)據(jù)表,然后再創(chuàng)建。但是刪除的時 候Magento沒有考慮到外鍵的關(guān)系,先嘗試刪除了主表,所以就有了以上異常。為了簡化教程的例子,我們暫時把 “createEntityTables”語句刪了。再次刷新頁面,你應(yīng)該看到正常的輸出。
?
給EAV模型添加數(shù)據(jù)
到這里為止,我們的EAV模型已經(jīng)創(chuàng)建好了,下面我們來為模型添加一些數(shù)據(jù)。在BlogController中添加以下方法
public function eavPopulateEntriesAction() { for($i=0;$i<10;$i++) { $weblog2 = Mage::getModel('helloworld-eav/eavblogpost'); $weblog2->setTitle('This is a test '.$i); $weblog2->save(); } echo 'Done'; } 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>'; }?
記得添加模型集合
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'); } }?
訪問以下URL
http://127.0.0.1/Magento/helloworld/blog/eavPopulateEntries
你應(yīng)該看到正確的輸出。細(xì)心一點(diǎn)的話你應(yīng)該發(fā)現(xiàn)這里有兩點(diǎn)比較特殊。第 一,“$weblog2->getCollection()->addAttributeToSelect('title')”,這里的 “title”是干什么的?因為EAV模型在數(shù)據(jù)庫層面比較復(fù)雜,一個簡單的查詢都需要好多個SQL才能完成。所以在查詢的時候你需要指明你想找什么,這 樣可以節(jié)省系統(tǒng)資源。不過你也可以傳入“*”,表示查找所有數(shù)據(jù)。第二,為什么“$this->_init”有兩個參數(shù)?在我們以前的章節(jié)中,簡單 模型的模型集合初始化的時候只需要傳入模型的URI就可以了,為什么這里要兩個參數(shù)呢?其實如果你仔細(xì)看了模型集合抽象類的代碼的話,你會發(fā)現(xiàn)這樣一段
if (is_null($resourceModel)) { $resourceModel = $model; }?
所以其實是需要模型的URI和資源模型的URI,但是由于我們前面章節(jié)的例子,這兩個URI是一樣的,所以省略了第二個參數(shù)。而這里,資源模型的URI和模型的URI是不一樣的,所以不能省略。
?
總結(jié)
到這里,你應(yīng)該對Magento整個系統(tǒng)的運(yùn)作有所了解了。起碼下一次你看到網(wǎng)店里面的某個商品部顯示了,或者什么屬性不對了,你知道去哪里找問題。除了本章介紹的內(nèi)容以外EAV模型還有很多東西可以學(xué)習(xí)。下面是我打算在以后的文章中介紹的一些內(nèi)容
- EAV屬性:EAV模型的屬性類型不局限于datatime, decimal, int, text和varchar。你可以創(chuàng)建自定義的數(shù)據(jù)類型。
- 集合篩選:對EAV模型的數(shù)據(jù)進(jìn)行篩選不是看起來的那么簡單,特別是當(dāng)屬性是自定義類型的情況下,我們需要在集合裝載之前調(diào)用“addAttributeToFilter”方法。
- Magento EAV模型繼承:Magento在基本的EAV模型之上又創(chuàng)建了模型的繼承關(guān)系,這些繼承關(guān)系可以和網(wǎng)店的功能直接相關(guān),也可以優(yōu)化EAV模型的查詢。
毫無疑問,EAV模型是Magento系統(tǒng)中最復(fù)雜的部分。不過你要始終相信一點(diǎn),不管多復(fù)雜,它也就是程序。從哲學(xué)角度來講,任何事物的產(chǎn)生都有特定的理由,你只需要搞清楚為什么。
?
?
from: http://www.zhlmmc.com/?p=665
?
?
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
