德鲁巴 7 |通过多个节点引用查询
Drupal 7 | Query through multiple node references
让我先从结构说起:
[main_node]
->field_reference_to_sub_node->[sub_node]
->field_ref_to_sub_sub_node->[sub_sub_node]
[sub_sub_node]
->field_type = ['wrong_type', 'right_type']
如何有效地查询所有 [sub_sub_node]
id right_type
,被 main_node
引用(这是当前打开的节点)?
在 foreach
上做 node_load 似乎有点矫枉过正。有人有更好的解决方案吗?非常感谢!
如果要直接查询table的字段:
$query = db_select('node', 'n')->fields('n_sub_subnode', array('nid'));
$query->innerJoin('table_for_field_reference_to_sub_node', 'subnode', "n.nid = subnode.entity_id AND subnode.entity_type='node'");
$query->innerJoin('node', 'n_subnode', 'subnode.subnode_target_id = n_subnode.nid');
$query->innerJoin('table_for_field_ref_to_sub_sub_node', 'sub_subnode', "n_subnode.nid = sub_subnode.entity_id AND sub_subnode.entity_type='node'");
$query->innerJoin('node', 'n_sub_subnode', 'sub_subnode.sub_subnode_target_id = n_sub_subnode.nid');
$query->innerJoin('table_for_field_type', 'field_type', "n_sub_subnode.nid = field_type.entity_id AND field_type.entity_type='node'");
$query->condition('n.nid', 'your_main_node_nid');
$query->condition('field_type.field_type_value', 'right_type');
这里是每一行的解释:
$query = db_select('node', 'n')->fields('n_sub_subnode', array('nid'));
我们首先使用别名 'n' 查询基节点 table。这是用于 'main_node' 的 table。然而,将返回的节点 ID 将来自另一个别名 (n_sub_subnode),您将在下面看到它。
$query->innerJoin('table_for_field_reference_to_sub_node', 'subnode', "n.nid = subnode.entity_id AND subnode.entity_type='node'");
第一个连接是 field_reference_to_sub_node 字段的 table,因此您必须将其替换为 table 的实际名称。这就是我们将如何获取对子节点的引用。
$query->innerJoin('node', 'n_subnode', 'subnode.subnode_target_id = n_subnode.nid');
为子节点连接回节点 table。您必须将 'subnode_target_id' 替换为 field_reference_to_sub_node table 中目标 ID 的实际字段。这个join的主要目的是确保子节点字段中有有效的节点。
$query->innerJoin('table_for_field_ref_to_sub_sub_node', 'sub_subnode', "n_subnode.nid = sub_subnode.entity_id AND sub_subnode.entity_type='node'");
table 的连接包含对 sub_sub_node 的引用,因此您必须将 'table_for_field_ref_to_sub_sub_node' 替换为 table 的实际名称。这就是我们如何获得对 sub_sub_node 的引用。
$query->innerJoin('node', 'n_sub_subnode', 'sub_subnode.sub_subnode_target_id = n_sub_subnode.nid');
为 sub_sub_node 连接回节点 table,以确保我们有有效的引用。您必须将 'sub_subnode_target_id' 替换为 'field_ref_to_sub_sub_node' table.
中目标 ID 的实际字段
$query->innerJoin('table_for_field_type', 'field_type', "n_sub_subnode.nid = field_type.entity_id AND field_type.entity_type='node'");
我们现在终于可以将 table 与 field_type 信息结合起来了。您必须将 'table_for_field_type' 替换为 table.
的实际名称
$query->condition('n.nid', 'your_main_node_nid');
如果需要,您现在可以为主节点 ID 添加条件。
$query->condition('field_type.field_type_value', 'right_type');
以及字段类型的条件。您必须将 'field_type_value' 替换为值的 table 字段的实际名称。
当然,如果你真的确定你总是有有效的引用,你可以跳过节点table的连接,直接使用目标id和tables连接字段entity_id 字段(基本上来自字段 table 的 target_id 必须是下一个字段的 entity_id)。
我真的希望我没有错别字,所以请仔细检查查询。
让我先从结构说起:
[main_node]
->field_reference_to_sub_node->[sub_node]
->field_ref_to_sub_sub_node->[sub_sub_node]
[sub_sub_node]
->field_type = ['wrong_type', 'right_type']
如何有效地查询所有 [sub_sub_node]
id right_type
,被 main_node
引用(这是当前打开的节点)?
在 foreach
上做 node_load 似乎有点矫枉过正。有人有更好的解决方案吗?非常感谢!
如果要直接查询table的字段:
$query = db_select('node', 'n')->fields('n_sub_subnode', array('nid'));
$query->innerJoin('table_for_field_reference_to_sub_node', 'subnode', "n.nid = subnode.entity_id AND subnode.entity_type='node'");
$query->innerJoin('node', 'n_subnode', 'subnode.subnode_target_id = n_subnode.nid');
$query->innerJoin('table_for_field_ref_to_sub_sub_node', 'sub_subnode', "n_subnode.nid = sub_subnode.entity_id AND sub_subnode.entity_type='node'");
$query->innerJoin('node', 'n_sub_subnode', 'sub_subnode.sub_subnode_target_id = n_sub_subnode.nid');
$query->innerJoin('table_for_field_type', 'field_type', "n_sub_subnode.nid = field_type.entity_id AND field_type.entity_type='node'");
$query->condition('n.nid', 'your_main_node_nid');
$query->condition('field_type.field_type_value', 'right_type');
这里是每一行的解释:
$query = db_select('node', 'n')->fields('n_sub_subnode', array('nid'));
我们首先使用别名 'n' 查询基节点 table。这是用于 'main_node' 的 table。然而,将返回的节点 ID 将来自另一个别名 (n_sub_subnode),您将在下面看到它。
$query->innerJoin('table_for_field_reference_to_sub_node', 'subnode', "n.nid = subnode.entity_id AND subnode.entity_type='node'");
第一个连接是 field_reference_to_sub_node 字段的 table,因此您必须将其替换为 table 的实际名称。这就是我们将如何获取对子节点的引用。
$query->innerJoin('node', 'n_subnode', 'subnode.subnode_target_id = n_subnode.nid');
为子节点连接回节点 table。您必须将 'subnode_target_id' 替换为 field_reference_to_sub_node table 中目标 ID 的实际字段。这个join的主要目的是确保子节点字段中有有效的节点。
$query->innerJoin('table_for_field_ref_to_sub_sub_node', 'sub_subnode', "n_subnode.nid = sub_subnode.entity_id AND sub_subnode.entity_type='node'");
table 的连接包含对 sub_sub_node 的引用,因此您必须将 'table_for_field_ref_to_sub_sub_node' 替换为 table 的实际名称。这就是我们如何获得对 sub_sub_node 的引用。
$query->innerJoin('node', 'n_sub_subnode', 'sub_subnode.sub_subnode_target_id = n_sub_subnode.nid');
为 sub_sub_node 连接回节点 table,以确保我们有有效的引用。您必须将 'sub_subnode_target_id' 替换为 'field_ref_to_sub_sub_node' table.
中目标 ID 的实际字段$query->innerJoin('table_for_field_type', 'field_type', "n_sub_subnode.nid = field_type.entity_id AND field_type.entity_type='node'");
我们现在终于可以将 table 与 field_type 信息结合起来了。您必须将 'table_for_field_type' 替换为 table.
的实际名称$query->condition('n.nid', 'your_main_node_nid');
如果需要,您现在可以为主节点 ID 添加条件。
$query->condition('field_type.field_type_value', 'right_type');
以及字段类型的条件。您必须将 'field_type_value' 替换为值的 table 字段的实际名称。
当然,如果你真的确定你总是有有效的引用,你可以跳过节点table的连接,直接使用目标id和tables连接字段entity_id 字段(基本上来自字段 table 的 target_id 必须是下一个字段的 entity_id)。
我真的希望我没有错别字,所以请仔细检查查询。