Magento模型的重寫規則

使用 Magento的 重寫 規則,你可以 很容易 擴展 Magento的 功能或 任何 可以添加 新的 模塊。 Magento的 遵循 MVC模式。 模型是 MVC的 主要形式 之一。 模型 通常 用於數據庫 連接和 各種邏輯 編碼。 Mogento 也使用 同樣的目的 模型。 每一個 模塊 有自己的 Magento的 模型類 但你可以 擴展或 修改 以下 的Magento 重寫 規則的任何 現有的模型 類。

 

假設 以滿足 您的項目 要求,您 需要更改或 提高客戶 的模型類 您可以更改 Mage_CUstomer_Model_Customer 類的代碼。 但它不是 一個 好的做法 的Magento 定製 Magento版本 升級 時, 那麼 有一個 刪除 您的自定義 代碼 的機會。 意味着你將 失去你的 變化。 實際上它 會爲你 可憐 !!!!! 因此,我們 應該遵循 的Magento 重寫 規則。 在這裏, 清楚 如何輕鬆地 覆蓋 你的各種 模型類。

 

Override Customer Model class:

第一次創建 新的模塊 下面的代碼 重寫 工作,並 寫在 app/etc/modules/ Yt_Customer.xml

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

做好 app/code/local/ Yt/Customer/etc/config.xml 配置
<?xml version="1.0"?>
<config>
<modules>
<Yt_Customer>
<version>0.1.0</version>
</Yt_Customer>
</modules>

<global>
<models>
<customer>
<rewrite>
<customer>Yt_Customer_Model_Customer</customer>
</rewrite>
</customer>
</models>
</global>







</config>

現在, 你的新 模型類 Yt_Customer_Model_Cutomer 並定義所有 覆蓋的方法
class Yt_Customer_Model_Customer extends Mage_Customer_Model_Customer
{
// override existing method
public function validate()
{
// Define new validate rules. From now magento call this validate method instead of existing method
//return $errors;
return true;
}

// You can create new method as you needed.
public function newMethod()
{
// function logic
}
}

如下面這樣 定義 app/code/ Yt/Customer/etc/config.xml 在這裏,您 將獲得 更清晰的概念 如何 覆蓋 各種型號 相同的XML
<?xml version="1.0"?>
<config>
<modules>
<Yt_Customer>
<version>0.1.0</version>
</Yt_Customer>
</modules>

<global>
<models>
<customer>
<rewrite>
<customer>Yt_Customer_Model_Customer</customer>
<address>Yt_Customer_Model_Address</address>
</rewrite>
</customer>
<customer_entity>
<rewrite>
<address>Yt_Customer_Model_Entity_Address</address>
</rewrite>
</customer_entity>
</models>
</global>
</config>








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章