简体   繁体   English

Drupal创建带有不存在节点的LEFT JOIN子选择的视图

[英]Drupal create views involving LEFT JOIN Sub-Select with non-existent node

i'm using Drupal 6 I have this table relation and I've translated into CCK complete with it's relation. 我正在使用Drupal 6,我具有此表关系,并且已将其转换为CCK。 Basically when I view a Period node, I have tabs to display ALL Faculty nodes combined with Presence Number. 基本上,当我查看“期间”节点时,我具有一些选项卡来显示“所有学院”节点以及“状态编号”。

here's the table diagram: http://i.stack.imgur.com/7Y5cU.png Translated into CCK like these: CCK Faculty (name), CCK Period (desc,from,to) and CCK Presence(node-reference-faculty, node-reference-period, presence_number) 这是表格图: http : //i.stack.imgur.com/7Y5cU.png转换为CCK的方式如下:CCK系(名称),CCK周期(从,到,到)和CCK在场(node-reference-faculty ,node-reference-period,presence_number)

Here's my simple manual SQL query that achieve this result: http://i.stack.imgur.com/oysd3.png 这是我达到此结果的简单手动SQL查询: http : //i.stack.imgur.com/oysd3.png

SELECT faculty.name, presence.presence_number FROM Faculty AS faculty
LEFT JOIN (SELECT * FROM Presence WHERE Period_id=1) AS presence ON faculty.id=presence.Faculty_id

The value of 1 for Period_id will be given by the Period Node ID from the url argument. Period_id的值1将由url参数中的Period Node ID给出。

Now the hardest part, is simulating simple SQL query above into Views. 现在最困难的部分是将上面的简单SQL查询模拟成View。 How can I make such query into Views in Drupal-6 or Drupal-7 ? 如何在Drupal-6或Drupal-7的Views中进行此类查询?

thanks for any helps. 感谢您的帮助。

The main issue, which I think you've noticed, is that if you treat Faculty as the base for your join, then there is no way to join on the Presence nodes. 我认为您已经注意到的主要问题是,如果将Faculty视为加入的基础,则无法在Presence节点上加入。 Oppositely, if you treat Presence as the base, then you will not see faculties that have no presence number. 相反,如果将“在场”作为基础,则不会看到没有在场编号的系。

There is no easy way, using your currently defined structure, to do these joins in views. 没有简单的方法可以使用您当前定义的结构在视图中进行这些连接。 I would say your easiest option is to remove the 'node-reference-faculty' field from the presence node and add a node-reference-presence field to the faculty. 我想说,最简单的选择是从状态节点中删除“ node-reference-faculty”字段,然后向该学院添加一个node-reference-presence字段。 Since CCK fields can have multiple values, you can still have your one-to-many relationship properly. 由于CCK字段可以具有多个值,因此您仍然可以正确地保持一对多关系。

The one downside of this is that then you need to manage the presence-faculty relationship from the faculty nodes instead of the presence nodes. 这样做的一个缺点是您需要从教师节点而不是在场节点来管理在场教师关系。 If that's a show stopper, which it could be depending on your workflow, you could have BOTH node-reference fields, and use a module like http://drupal.org/project/backreference to keep them in sync. 如果那是一个显示停止器(可能取决于您的工作流程),则可以同时具有两个节点引用字段,并使用http://drupal.org/project/backreference之类的模块来使其保持同步。

Once you have your reference from faculty -> presence, you will need to add a relationship in Views. 从教职员工处获得引用->在场后,您将需要在视图中添加关系。 Just like adding a field or a filter, open the list of relationships and find the one for your node-reference field. 就像添加字段或过滤器一样,打开关系列表并为您的节点引用字段找到一个关系。

Next, you will need to add an argument for period id and set it up to use a node id from the url. 接下来,您将需要为期间ID添加一个参数,并将其设置为使用URL中的节点ID。 The key thing is that when you add the argument, it will ask which relationship to use in its options. 关键是,当您添加参数时,它将询问在其选项中使用哪种关系。 You will want to tell it to use your newly added presence relationship. 您将要告诉它使用您新添加的状态关系。

You don't really need to do a subquery in your SQL like that. 您实际上不需要像这样在SQL中执行子查询。 This should be the same thing and won't make mysql try to create a temporary table. 这应该是同一件事,并且不会使mysql尝试创建临时表。 I mention it because you can't really do subqueries in Views unless you are writing a very custom Views handler, but in this case you don't really need the subquery anyway. 我提到它是因为除非您正在编写非常自定义的Views处理程序,否则您实际上无法在Views中执行子查询,但是在这种情况下,您实际上并不需要子查询。

Ex. 例如

SELECT f.name, p.presence_number
FROM Faculty AS f
LEFT JOIN Presence AS p ON f.id=p.Faculty_id
WHERE p.Period_id=1;

I wrote an article about how to achieve a similar outcome here. 我在这里写了一篇有关如何获得类似结果的文章。 http://scottanderson.com.au/#joining-a-views-query-to-a-derived-table-or-subquery http://scottanderson.com.au/#joining-a-views-query-to-a-derived-table-or-subquery

Basically how to alter a Views query to left join on a sub-query. 基本上,如何将Views查询更改为在子查询上左联接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM