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>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值