在Redmine核心视图中添加额外的SQL查询(覆盖插件中的视图)

发布于 2024-08-12 04:27:22 字数 1226 浏览 6 评论 0原文

我正在覆盖插件中的 Redmine 核心视图 (/views/reports/_details.rhtml)。只是为了打印一些额外的数据,例如已分配和未分配的未决问题数。

我尝试重写控制器并添加一个方法来执行此操作,但我从未让它工作,因此我将下面的代码添加到视图中(是的,它很丑陋,但页面很少被使用)。

现在我的问题是,视图在所有跟踪器 (row.id) 中循环并显示信息,例如有多少问题处于打开和关闭状态。所以我添加了一个额外的 SQL 查询,但它只适用于第一次跟踪器迭代,其余的它会一遍又一遍地显示相同的数据。

当我查看development.log 时,其中只有一个SQL 查询。但是当我输出 row.id (<%= row.id %>) 时,它会显示每个跟踪器的正确值。

我应该如何解决这个问题?

_details.rhtml 中的代码

<% @total_assigned_and_open ||=
        ActiveRecord::Base.connection.select_all("select count(i.id) as total
        from
        #{Issue.table_name} i, #{IssueStatus.table_name} s, #{Tracker.table_name} t
        where
        i.status_id=s.id
        and i.tracker_id=#{row.id}
        and i.project_id=#{@project.id}
        and i.assigned_to_id is null
        and s.is_closed = 0
        group by s.id, s.is_closed, t.id limit 1") %>

development.log 中的 SQL 查询

select count(i.id) as total
from
issues i, issue_statuses s, trackers t
where
i.status_id=s.id
and i.tracker_id=1
and i.project_id=1
and i.assigned_to_id is null
and s.is_closed = 0
group by s.id, s.is_closed, t.id limit 1

(之前从未使用过 Ruby on Rails 或 Redmine...)

I'am overriding a Redmine core view in my plugin (/views/reports/_details.rhtml). Just to print some extra data, like how many open issues are assigned and not assigned.

I tried to override the controller instead and add a method to do this, but I never got it to work, so I added the code below to the view (yes, it's ugly, but the page will rarely by used).

Now to my problem, the view are looping trough all trackers (row.id) and showing information, like how many issues are open and closed. So I have added a extra SQL query, but it only works for the first tracker iteration, for the rest it shows the same data over and over again.

When I look at the development.log there are only one SQL query in it. But when I output row.id (<%= row.id %>) it shows the correct value for each tracker.

How should i solve this?

Code in _details.rhtml

<% @total_assigned_and_open ||=
        ActiveRecord::Base.connection.select_all("select count(i.id) as total
        from
        #{Issue.table_name} i, #{IssueStatus.table_name} s, #{Tracker.table_name} t
        where
        i.status_id=s.id
        and i.tracker_id=#{row.id}
        and i.project_id=#{@project.id}
        and i.assigned_to_id is null
        and s.is_closed = 0
        group by s.id, s.is_closed, t.id limit 1") %>

SQL query in development.log

select count(i.id) as total
from
issues i, issue_statuses s, trackers t
where
i.status_id=s.id
and i.tracker_id=1
and i.project_id=1
and i.assigned_to_id is null
and s.is_closed = 0
group by s.id, s.is_closed, t.id limit 1

(Never used Ruby on Rails or Redmine before...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2024-08-19 04:27:22

我通过添加两个 SQL 查询(在视图中)来解决这个问题,该查询选择所选项目的所有问题。

<% @all_unsigned_issues ||=
ActiveRecord::Base.connection.select_all("select i.status_id as status, s.is_closed as 
closed, t.id as tracker_id, count(i.id) as total
from
#{Issue.table_name} i, #{IssueStatus.table_name} s, #{Tracker.table_name} t 
where
i.assigned_to_id is null
and i.project_id=#{@project.id}
and i.status_id=s.id
and i.tracker_id=t.id
group by i.id") %>

然后我用。

<%= aggregate_link @all_unsigned_issues, { "tracker_id" => row.id, "status" => 5 },
            :controller => 'issues', :action => 'index', :project_id => ((row.is_a?(Project) ? row : @project)) %>

I solved it by adding two SQL queries (in the view) that selects all issues to the selected project.

<% @all_unsigned_issues ||=
ActiveRecord::Base.connection.select_all("select i.status_id as status, s.is_closed as 
closed, t.id as tracker_id, count(i.id) as total
from
#{Issue.table_name} i, #{IssueStatus.table_name} s, #{Tracker.table_name} t 
where
i.assigned_to_id is null
and i.project_id=#{@project.id}
and i.status_id=s.id
and i.tracker_id=t.id
group by i.id") %>

Then I use.

<%= aggregate_link @all_unsigned_issues, { "tracker_id" => row.id, "status" => 5 },
            :controller => 'issues', :action => 'index', :project_id => ((row.is_a?(Project) ? row : @project)) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文