添加(或覆盖)cakePHP 中的核心 class 方法

Adding to (or overriding) a core class method in cakePHP

我们正在使用 cakePHP 2.7.x。

我的问题是覆盖核心 class——或者专门向方法添加功能而不修改核心代码。

我需要为该方法添加一些功能: cake/lib/Cake/Model/Datasource/DboSource->BuildColumn()

文档说把整个class放在app下的同一个位置,然后App loader会加载它。所以我将 class 复制到 marketplace/Model/Datasource/DboSource 并添加了我的代码片段。不幸的是,一切都没有改变。

如果我直接将我的更改添加到原始方法,一切正常。

上下文: 在该方法中,所有默认值都被不加区别地引用。因此,像 DEFAULT CURRENT_TIMESTAMP 这样的东西被解释为 运行 被解释为 DEFAULT 'CURRENT_TIMESTAMP' ,这显然不能按预期工作。 具体来说,它在尝试烘烤夹具时给我错误 "Syntax error or access violation: 1067 Invalid default value for 'created'"

我被引导到问题的原因,以及针对 cakePHP 1.2.6 的建议修复,此处: http://mamchenkov.net/wordpress/2010/04/21/unit-tests-with-cakephp/

根据 docs:

You can override almost every class in the framework, exceptions are the App and Configure classes. Whenever you like to perform such overriding, just add your class to your app/Lib folder mimicking the internal structure of the framework

所以路径应该是marketplace/Lib/Model/Datasource/DboSource,而不是marketplace/Model/Datasource/DboSource

很明显我的流浪机器出了问题。在我重新启动之后,就像 Bruno Ferriera 概述的那样完美地工作!