为什么magento的rewrite方法对抽象类无效

2023-05-16

     magento中,是没法通过Mage::getModel("xx/xx")配合xml中的<rewrite>实现abstruct class的rewrite。 为什么?这需要详细了解一下magento中model的rewrite原理。
    

     假设xml中我们定义了如下的rewrite:

        	<customer>
        		<rewrite>
        			<customer>Newjueqi_Customer_Model_Customer</customer>
        		</rewrite>
        	</customer>

    根据配置,我们是把 Mage_Customer_Model_Customer rewrite为 Newjueqi_Customer_Model_Customer。
    那么,在执行 Mage::getModel('customer/customer') 时,magento到底做了什么?我们分析一下源码:

    1. 首先来到getmodel方法

    public static function getModel($modelClass = '', $arguments = array())
    {
	    //获取class实例
        return self::getConfig()->getModelInstance($modelClass, $arguments);
    }

     2. 看一下magento是怎么生成实例的

    public function getModelInstance($modelClass='', $constructArguments=array())
    {
	//获取类的名称,rewrite的关键就在与怎么把原来要生成的类名Mage_Customer_Model_Customer 换成 Newjueqi_Customer_Model_Customer。
        $className = $this->getModelClassName($modelClass);
        if (class_exists($className)) {
            Varien_Profiler::start('CORE::create_object_of::'.$className);
			
            //通过new的方法来产生一个类的实例
            $obj = new $className($constructArguments);
						
            Varien_Profiler::stop('CORE::create_object_of::'.$className);
            return $obj;
        } else {
            /* throw Mage::exception(
                'Mage_Core',
                Mage::helper('core')->__('Model class does not exist: %s.', $modelClass)
            ); */
            return false;
        }
    }


3. 好,最后看一下magento是怎么把生成的类名Mage_Customer_Model_Customer 换成 Newjueqi_Customer_Model_Customer。

    //对类名检验
    public function getModelClassName($modelClass)
    {
        $modelClass = trim($modelClass);
        if (strpos($modelClass, '/')===false) {
            return $modelClass;
        }
        return $this->getGroupedClassName('model', $modelClass);
    }	
	
    //获取所生成class的实际类名
    public function getGroupedClassName($groupType, $classId, $groupRootNode=null)
    {
        if (empty($groupRootNode)) {
            $groupRootNode = 'global/'.$groupType.'s';
        }

        $classArr = explode('/', trim($classId));
        $group = $classArr[0];
        $class = !empty($classArr[1]) ? $classArr[1] : null;

        if (isset($this->_classNameCache[$groupRootNode][$group][$class])) {
            return $this->_classNameCache[$groupRootNode][$group][$class];
        }

        $config = $this->_xml->global->{$groupType.'s'}->{$group};

        // First - check maybe the entity class was rewritten
        $className = null;
		
	/*
	$className = (string)$config->rewrite->$class;
	这里就是rewrite最关键的地方,magento已经在xml中找到“customer/customer”,
	接着magento会找一下<customer>标签下是否有<rewrite>标签,如果找到的话,就会把rewrite标签中的类名替换实际类名
		
		<customer>
        	  <rewrite>
        	     <customer>Newjueqi_Customer_Model_Customer</customer>
        	  </rewrite>
        	</customer>
			
	从上面的xml配置可知,是把“customer/customer”确定为Newjueqi_Customer_Model_Customer
         */
        if (isset($config->rewrite->$class)) {
            $className = (string)$config->rewrite->$class;
        } else {
            /**
             * Backwards compatibility for pre-MMDB extensions.
             * In MMDB release resource nodes <..._mysql4> were renamed to <..._resource>. So <deprecatedNode> is left
             * to keep name of previously used nodes, that still may be used by non-updated extensions.
             */
            if ($config->deprecatedNode) {
                $deprecatedNode = $config->deprecatedNode;
                $configOld = $this->_xml->global->{$groupType.'s'}->$deprecatedNode;
                if (isset($configOld->rewrite->$class)) {
                    $className = (string) $configOld->rewrite->$class;
                }
            }
        }

        // Second - if entity is not rewritten then use class prefix to form class name
        if (empty($className)) {
            if (!empty($config)) {
                $className = $config->getClassName();
            }
            if (empty($className)) {
                $className = 'mage_'.$group.'_'.$groupType;
            }
            if (!empty($class)) {
                $className .= '_'.$class;
            }
            $className = uc_words($className);
        }

        $this->_classNameCache[$groupRootNode][$group][$class] = $className;
        return $className;
    }

看了上面的源码分析,我们可总结一下magento的rewrite机制:
1. 在xml文件中构造Mage::getModel('xxx/xxx')中所对应'xxx/xxx'所对应的实际类名,如果<rewrite>标签存在就使用<rewrite>标签中的类名,否则就用实际类名。
2. 使用$obj = new $className($constructArguments) 生成类的实例。

 由以上的分析可知,magento的rewrite最后是调用 new 来生成一个类的实例,但在php中,abstruct class是没法生成类的实例的,所以abstruct class是没法rewrite。

【文章作者】曾健生

【作者邮箱】zengjiansheng1@126.com

【作者QQ】190678908

【作者博客】blog.csdn.net/newjueqi






本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么magento的rewrite方法对抽象类无效 的相关文章

  • 在 Magento 中以编程方式创建 CMS/页面

    我看到了下面这个帖子的回复Magento 静态 CMS 块存储在哪里 https stackoverflow com questions 4932652 where are magento static cms blocks stored关
  • 将附加模板添加到右侧边栏 magento

    我只是尝试添加额外的模板文件以在右侧栏中包含内容块 但失败了 下面是我的努力 添加到 local xml 文件中
  • 产品和报价项目之间的概念区别是什么

    涉及班级 Mage Sales Model Quote Item and 法师 目录 型号 产品 我通过监听事件 在购物车添加上 得到了它们 我正在尝试从外部来源更新产品的数量信息 到目前为止 我的代码仅基于产品信息 我不确定这是否正确 报
  • Magento 购物车未更新阿拉伯语商店视图中的数量

    我在 Magento 1 8 1 安装中遇到以下问题 我有两种商店视图 英语 默认 和阿拉伯语 在英文商店视图中 如果我将产品添加到购物车 我可以通过在数量框中输入新数量并单击更新购物车来修改数量 这会更改数量和总数 但是 当我切换到阿拉伯
  • 如何配置 Magento Enterprise 使用 Solr 作为主要搜索引擎?

    我尝试寻找文档 但它非常稀疏 我编写了以下指南 希望它可以帮助人们节省 2 3 个小时的安装 设置和配置时间 与 MySQL 全文搜索标准设置相比 使用 Solr 带来的性能提升非常惊人 在您的 Magento 安装中让它工作绝对值得花时间
  • PHP foreach 循环外访问变量

    我是 PHP 新手 谁能告诉我如何访问 foreach 之外的 foreach 循环变量 请通过代码查找如下 我需要它在下面的 html img 标签的 src 属性中
  • 无法登录 Magento 管理员

    我在登录我们的一个临时站点上的 Magento 管理面板时遇到问题 它在我们的 webdev 服务器上 100 工作 不久前在临时服务器上也工作得很好 我做了一些研究 大多数人认为这与在本地主机上运行 Magento 以及浏览器不为域名中没
  • 查找商店的根类别

    我正在使用 Magento 1 6 1 版 我需要获取商店的根类别 我在谷歌中搜索没有得到任何好的想法 代码 请问如何获取商店的根类别 Mage app gt getStore gt getRootCategoryId 上面的代码给出了默认
  • magento - 无法与 PayPal 网关通信

    有什么解决办法吗 我已经在配置 gt gt 系统 gt gt 支付方式 gt gt PayPal支付解决方案中禁用了SSL验证 但还是不行 您需要禁用 SSL 验证 进入后台 系统 gt 配置 gt 付款方式 找到PayPal Expres
  • Magento CSRF 保护

    我正在 Magento 中查看自定义表单 我看到了这些教程 http fastdivision com 2012 03 29 diy magento create ajax login registration forms for your
  • Magento Connect 登录后尝试更改为什么路径?

    我每次都会收到错误消息 部署 FTP 错误 登录后无法 chdir 我通过 chmod ing 我的 complete path to magento installation dir 成功完成了第一次连接和设置下载器 至 0777 在 d
  • 如何在注册和结账过程中更改magento中的“送货信息”标签

    我想将 帐单信息 标签文本更改为 运输和帐单信息 我尝试使用 Mage Checkout csv 但这没有帮助 请提出解决方案 谢谢你 Use the 翻译文件translate csv在你的主题中 出于演示目的 我将使用默认包 app d
  • Magento:两种基于重量的统一运费

    尝试按如下方式设置我的运费 如果购物车总重量低于 1 公斤 则运费为 3 68 英镑 如果购物车总重量超过 1 公斤 则运费为 6 68 英镑 如果购物车总价超过 100 英镑 则免运费 我设法使用 6 68 英镑的统一费率将所有这些结合在
  • 如何覆盖magento中的管理模板文件?

    我需要覆盖 adminhtml sales order create items grid phtml 文件 以在从管理员创建新订单时在每个项目下显示一些自定义文本 我希望通过自定义模块来完成此操作 任何人都可以建议如何覆盖管理模板文件 非
  • Magento Rest API - oAuth 错误

    我是第一次使用 Magento Rest API 我浏览了 Rest API 的教程http www magentocommerce com api rest并尝试了一个通过 API 获取产品的示例 callbackUrl http loc
  • 一旦我们点击取消按钮,文本字段就会隐藏

    我们正在使用 magento 多供应商网站 我们正在使用以下代码来更新和取消价格 但是一旦我们单击 取消 按钮 文本字段就会隐藏 PHTML span class label pro status span
  • 在 Magento 控制器中使用 move_uploaded_file

    我是 magento 的新手 我正在 magento 管理中创建用于文件上传的自定义模块 现在我已将上传文件发布到我的模块控制器中 这里我用过move uploaded file将文件上传到与控制器文件夹相同的目录中 下面的代码我用于控制器
  • 通过列计数拆分时重复表头

    我正在 Magento 中输出产品列表 作为包装在表格中的简单列表 由于此列表可能会很长 100 个以上产品 因此我使用了来自这里的想法 https stackoverflow com questions 21001803 how to h
  • Magento 设置脚本中的 ALTER TABLE 不使用 SQL

    乔纳森 戴 https stackoverflow com users 336905 jonathan day says 更新不应采用以下形式 SQL命令 我没遇到过 任何 DDL 或 DML 语句不能 通过 Magento 的配置执行 结
  • Magento EE FPC 中的打孔法师_目录_块_产品_价格

    我花了很长时间找出代码 参数来为Mage Catalog Block Product Price块在magento中打孔全页缓存 我可以在第一次加载页面时显示价格 但是当缓存 id 是唯一的时 它不会正确呈现价格 当它应该被缓存时 它会正确

随机推荐

  • doxygentoolkit.vim 用法

    http blog chinaunix net space php uid 61 20570759 amp do 61 blog amp id 61 1922274 早就安上了这个东西 xff0c 只是一直没研究它怎么用 因为 emacs
  • 一些vim知识的摘录

    h function list 看内置函数 b name variable local to a buffer w name variable local to a window g name global variable also in
  • 为什么 Vim 使用 HJKL 键作为方向键

    出处 xff1a http blog jobbole com 18650 导读 xff1a 关于这个问题 xff0c 以前网络上有一种说法 xff0c 手指放在键盘上输入时 xff0c HJKL 比方向键距离手指更近 xff0c 自然输入效
  • 蚂蚁变大象:浅谈常规网站是如何从小变大的(一)

    http zgwangbo blog 51cto com 4977613 849529 标签 xff1a 架构 web 原创作品 xff0c 允许转载 xff0c 转载时请务必以超链接形式标明文章 原始出处 作者信息和本声明 否则将追究法律
  • 【转】高效使用vim

    出处 xff1a http www cnblogs com hyddd archive 2010 04 08 1706863 html 英文出处 xff1a jmcpherson org editing html 翻译引用 xff1a ti
  • 浅谈HTTP中Get与Post的区别

    http www cnblogs com hyddd archive 2009 03 31 1426026 html Http定义了与服务器交互的不同方法 xff0c 最基本的方法有4种 xff0c 分别是GET xff0c POST xf
  • PID算法搞不懂?看这篇文章就够了。

    点击上方 大鱼机器人 xff0c 选择 置顶 星标公众号 福利干货 xff0c 第一时间送达 xff01 转自知乎 xff1a jason 原文链接 xff1a https zhuanlan zhihu com p 74131690 1 目
  • Http Message结构学习总结

    http www cnblogs com hyddd archive 2009 04 19 1438971 html 最近做的东西需要更深入地了解Http协议 xff0c 故死磕了一下RFC2616 xff0d HTTP 1 1协议 xff
  • 浅析数字证书

    出处 xff1a http www cnblogs com hyddd archive 2009 01 07 1371292 html hyddd原创 xff0c 转载请说明出处 gt 最近看会Session hijack的东西 xff0c
  • Cookie小记

    出处 xff1a http www cnblogs com hyddd archive 2008 12 26 1363229 html 最近在工作上经常看一些安全相关的东西 xff0c Cookie以前看过 xff0c 但了解不深 xff0
  • Session小记

    出处 xff1a http www cnblogs com hyddd archive 2008 12 29 1364646 html 看了一些Session的资料 xff0c 在这里再总结一下Session相关的知识 很多是从网上搜集的资
  • 点击<a href="#"/>后屏幕滚动问题

    问 xff1a 当 lt a href 61 34 34 gt 点击后屏幕会滚动到最上面 xff0c 有啥办法不让屏幕滚动 xff1f 答 xff1a href 61 34 javascript void 0 34 或 nclick 61
  • 内存管理知识

    原创作品 xff0c 允许转载 xff0c 转载时请务必以超链接形式标明文章 原始出处 作者信息和本声明 否则将追究法律责任 http xqtesting blog 51cto com 4626073 808548 一般的程序语言 xff0
  • 用户体验:别让我想,别让我停!

    http xqtesting blog 51cto com 4626073 813561 在交互设计中 xff0c 存在着几条普遍的法则令网页设计更有效 最重要的一条是 别让我思考 xff0c 越简洁越好 比如不要因为奇怪的表达方式强迫用户
  • MySQL慢查询的两种分析方案 slow sql

    http blog csdn net ylqmf article details 6541542 前一段日子 xff0c 我曾经设置了一次记录在MySQL数据库中对慢于1秒钟的SQL语句进行查询 想起来有几个十分设置的方法 xff0c 有几
  • 如何使用SQL Profiler 性能分析器

    http blog csdn net ylqmf article details 6541625 ysql 的 sql 性能分析器主要用途是显示 sql 执行的整个过程中各项资源的使用情况 分析器可以更好的展示出不良 SQL 的性能问题所在
  • magento中生成https链接的简单方法

    有关magento中https的基础知识 xff0c 请看 magento中的启用https 如果是在项目的后期才决定采用https xff0c 那么就要面临一个问题 xff1a 大量的生成url的代码需要修改 xff0c 这是一个很大的工
  • 树莓派无屏幕连接WiFi

    将刷好 Raspbian 系统的 SD 卡用电脑读取 在 boot 分区 xff0c 也就是树莓派的 boot 目录下新建 wpa supplicant conf 文件 xff0c 按照下面的参考格式填入内容并保存 wpa supplica
  • MySQL数据库存储引擎MyISAM和InnoDB的对比详解

    http www mysqlops com 2011 12 09 myisam E5 92 8Cinnodb E5 AF B9 E6 AF 94 E8 AF A6 E8 A7 A3 html 之前Eugene兄已经写过两篇关于myisam转
  • 为什么magento的rewrite方法对抽象类无效

    magento中 xff0c 是没法通过Mage getModel 34 xx xx 34 配合xml中的 lt rewrite gt 实现abstruct class的rewrite 为什么 xff1f 这需要详细了解一下magento中