magento 重写不起作用
我创建了一个模块,该模块使用自定义小部件/网格/列作为我的管理网格来显示缩略图,并且一切正常。现在我需要在同一项目中创建另一个模块来执行相同的操作,但图像列将不起作用。看起来它甚至没有加载我的新类,因为它不会执行我放入文件中的任何 stmt。我知道它正确加载数据,b/c 如果我将类型更改为文本,那么数据库中的正确信息将填充到该字段中。但是当我更改为新的“图像”类型时,单元格为空。有谁知道为什么它不起作用?
ABT/Background/etc/config.xml <<不起作用
<global>
....
<blocks>
<background>
<class>ABT_Background_Block</class>
</background>
<adminhtml>
<rewrite>
<widget_grid_column>ABT_Background_Block_Widget_Grid_Column</widget_grid_column>
</rewrite>
</adminhtml>
</blocks>
....
</global>
我复制来启动和运行的模块具有完全相同的配置设置,但它工作正常 ABT/Feature/etc/config.xml <<这工作正常
<global>
....
<blocks>
<feature>
<class>ABT_Feature_Block</class>
</feature>
<adminhtml>
<rewrite>
<widget_grid_column>ABT_Feature_Block_Widget_Grid_Column</widget_grid_column>
</rewrite>
</adminhtml>
</blocks>
....
</global>
I created a module that used a custom widget/grid/column for my admin grid to display a thumbnail image and everything worked. Now I need to create another module in the same project that does the same thing, but the image column will not work. It appears like its not even loading my new class, since it won't execute any stmt I put into the file. I know its properly loading data, b/c if I change the type to text, then the correct info from the DB is populated into the field. But when I change to my new 'image' type the cell is empty. does anyone know why it wouldn't be working?
ABT/Background/etc/config.xml << doesn't work
<global>
....
<blocks>
<background>
<class>ABT_Background_Block</class>
</background>
<adminhtml>
<rewrite>
<widget_grid_column>ABT_Background_Block_Widget_Grid_Column</widget_grid_column>
</rewrite>
</adminhtml>
</blocks>
....
</global>
the module I copied to get me up and running has the exact same config setup, but yet it works fine
ABT/Feature/etc/config.xml << this works correctly
<global>
....
<blocks>
<feature>
<class>ABT_Feature_Block</class>
</feature>
<adminhtml>
<rewrite>
<widget_grid_column>ABT_Feature_Block_Widget_Grid_Column</widget_grid_column>
</rewrite>
</adminhtml>
</blocks>
....
</global>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您的设置是否正确。
不管怎样,看起来你在同一个类
Widget_Grid_Column
上有两个覆盖规则。最后加载的模块是ABT_Feature
,因此该块的规则会覆盖ABT_Background
模块的规则。您有两种解决方案:
ABT_Feature_Block_Widget_Grid_Column
类扩展ABT_Background_Block_Widget_Grid_Column
代码>.I'm not sure to have got your setup correctly.
Anyway, it looks like you have two override rules on the same class
Widget_Grid_Column
. The last module loaded is theABT_Feature
so the rule of that block overwrite the rule of theABT_Background
module.You have two solutions:
ABT_Feature_Block_Widget_Grid_Column
class will extend theABT_Background_Block_Widget_Grid_Column
.以下是当您重写类时 Magento 会发生的情况。
当 Magento 实例化一个 Block 类时,它使用类似以下的代码
createBlock
方法是一个工厂。 Magento 使用标识符来查找应该实例化哪个类。默认情况下,
当您创建重写时,您是在告诉 Magento
这意味着,对于任何给定系统,类只能重写一次。在上面显示的代码中,您尝试重写该类两次。只有您的重写之一会获胜。
我采取的快速方法是将您的所有自定义保留在单个覆盖类中。
更一般地说,如果可能的话,我会尽量避免使用重写。它们是一个强大的系统,但应谨慎使用。我没有做过太多的网格定制,但我尝试采取的一般方法是更改 adminhtml 布局以从我的自定义模块实例化一个新的网格类,该模块扩展现有的网格类,这反过来又可以使用自定义列类。
前期需要做更多的工作,但是一旦你弄清楚了,你就可以一遍又一遍地使用该技术,而不必担心重写带来的冲突。
Here's what happening with Magento when you rewrite a class.
When Magento instantiates a Block class, it uses code something like the following
The
createBlock
method is a factory. Magento uses the identifierto lookup which class should be instantiated. By default, that's
When you create your rewrite, you're telling Magento
This means that, for any given system, a class may only be rewritten once. In the code you're showing above, you're trying to rewrite the class twice. Only one of your rewrites will win.
The quick approach I'd take is to keep all your customizations in a single override class.
More generally, I try to avoid using rewrites if at all possible. They're a powerful system, but should be using sparingly. I haven't done much grid customizing, but the general approach I'd try to take would be to change the adminhtml layout to instantiate a new grid class from my custom module that extends an existing grid class, which in turn may use custom column classes.
More work up front, but once you've figured it out you can use the technique over and over again, and not worry abou conflicts from a rewrite.