使用 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>