Magento模型的重写规则

来源:互联网 发布:手机mac修改软件 编辑:程序博客网 时间:2024/04/28 17:58

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








原创粉丝点击