Drupal 视图:将关系的空结果渲染为 0
我在 Drupal 中配置了一个视图来返回节点,并按平均投票降序对它们进行排序。就视图而言,平均投票的值是一种关系。我注意到没有投票的节点显示在平均值为负的节点之后。没有投票的节点的平均值应该为 0,但我相信 MySQL JOIN
导致返回 NULL
值(因为连接表中没有匹配的行,因为在对该项目进行第一次投票后会创建一行)。
我发现使用 MySQL 可以使用 IFNULL(column_name,'other value') 将列中的所有 NULL
值输出为另一个值。
我觉得我需要修改 Views 模块才能获得此功能,但我希望有某种选项可以在关系中返回 NULL 值(关系不存在的项目)作为 0 而不是 NULL ,以便我可以正确对节点进行排序。
我使用的模块包括 Views、Voting API、Vote Up/Down 和 CTools。
谢谢。
I have a View configured in Drupal to return nodes, sorting them by their average vote in descending order. For the purpose of the View, the value of the average votes is a Relationship. I noticed that nodes with no votes are displayed after nodes with a negative average. Nodes with no votes should have an average of 0, but I believe the MySQL JOIN
is causing NULL
values to be returned (as there are no matching rows in the joined table, since a row is created after the first vote is cast for that item).
I discovered that with MySQL it is possible to output all values that are NULL
in a column as another value with IFNULL(column_name,'other value')
.
I feel like I would need to modify the Views module in order to obtain this functionality, but I'm hoping that there is some sort of option that returns NULL
values in a relation (a relation doesn't exist for the item) as 0 instead of NULL
, so that I can properly sort the nodes.
The modules I am using include Views, Voting API, Vote Up/Down, and CTools.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哦,请不要直接修改视图。
写个插件就可以了。它都是面向对象的,因此您只需编写一个字段处理程序来扩展该字段的现有处理程序。
hook_views_handlers
是开始的地方。您需要做的就是复制粘贴现有处理程序,对 SQL 进行调整,并为该字段指定一个新名称。它会像其他任何字段一样显示在您的可用字段列表中。
-- 编辑 --
如果您以前从未这样做过,那么视图文档一开始可能会有点令人困惑,但一旦您明白“啊哈!”,它就会变得超级简单。片刻。
一个好的起点是查看其他扩展视图的模块,并复制他们的技巧。文档位于 http://views2.logrus.com/doc/html/index.html< /a> 将帮助您了解对象是如何映射的。
就我个人而言,我会在
->render()
方法而不是->query()
方法中进行修改。这样,您只需添加一个简单的if (is_null($value)) {$value = 0;}
而不必使用 SQL 技巧。Oooh, please don't modify Views directly.
Just write a plugin. It's all object-oriented, so you just write a field handler that extends the exiting handler for that field.
hook_views_handlers
is the place to start for that.All you'll need to do is copy-paste the existing handler, make your tweaks to the SQL, and give the field a new name. It'll show up in your list of available fields like any other.
-- Edit --
Views documentation can be a little confusing at first if you've never done it before, but it's super-simple once you get that "ahah!" moment.
A good place to start would be looking at other modules that extend views, and copying their tricks. The docs at http://views2.logrus.com/doc/html/index.html will help you get a sense of how the objects are mapped.
Personally, I would make the modification in the
->render()
method instead of in the->query()
method. That way you can just add a simpleif (is_null($value)) {$value = 0;}
instead of using SQL tricks.