【问题标题】:Best way to search multiple fields with entity field query?使用实体字段查询搜索多个字段的最佳方法?
【发布时间】:2023-04-09 07:58:01
【问题描述】:

使用实体字段查询搜索多个字段的最佳方式是什么?我有一个名为“项目”的内容类型,它具有“项目经理”和“开发人员”的自定义字段,它们是对用户的引用,我试图在每个用户的个人资料上显示给定用户与相关联的项目列表。

这是我目前所拥有的:

<?php
  // Print all projects associated with this user
  $profile = user_load(arg(1));
  // Check database for reference to this user in pm field
  $query = new EntityFieldQuery();
  $result = $query->entityCondition('entity_type', 'node')
          ->entityCondition('bundle', 'project')
          ->propertyCondition('status', 1)
          ->fieldCondition('<insert multiple fields here..?>', 'target_id', $profile->uid, '=')
          ->execute();


  if (!empty($result['node'])) {
    $nids = array_keys($result['node']);
    $nodes = node_load_multiple(array_keys($result['node']));
    echo "<b>User's projects:<br></b>";
  }
  // display projects
  foreach ($nodes as &$node) {
    $targetPath = "content/";
    $targetPath .= str_replace(' ', '-', $node->title);
    $targetPath = 'http://'.$_SERVER['HTTP_HOST'].base_path().drupal_get_path_alias($targetPath, $path_language = '');
    echo "<a href='$targetPath'>$node->title</a><br>";
  }

?>

【问题讨论】:

    标签: php mysql database drupal


    【解决方案1】:

    您似乎需要根据 [field_1] OR [field_2] 值加载用户。

    <?php
        // Print all projects associated with this user
        $profile = user_load(arg(1));
        // Check database for reference to this user in pm field
        $query = new EntityFieldQuery();
        $result_1 = $query->entityCondition('entity_type', 'node')
            ->entityCondition('bundle', 'project')
            ->propertyCondition('status', 1)
            ->fieldCondition('field_1', 'target_id', $profile->uid, '=')
            ->execute();
    
        $query = new EntityFieldQuery();
        $result_2 = $query->entityCondition('entity_type', 'node')
            ->entityCondition('bundle', 'project')
            ->propertyCondition('status', 1)
            ->fieldCondition('field_2', 'target_id', $profile->uid, '=')
            ->execute();
    
        $results = array();
    
        // 1st Set
        if (!empty($result_1['node'])) {
            $results = array_keys($result_1['node']);
        }
        // 2nd Set
        if (!empty($result_2['node'])) {
            $results += array_keys($result_2['node']);
        }
    
        if (count($results)) {
                $nodes = node_load_multiple($results);
                echo "<b>User's projects:<br></b>";
    
                // display projects
                foreach ($nodes as &$node) {
                    $targetPath = "content/";
                    $targetPath .= str_replace(' ', '-', $node->title);
                    $targetPath = 'http://'.$_SERVER['HTTP_HOST'].base_path().drupal_get_path_alias($targetPath, $path_language = '');
                    echo "<a href='$targetPath'>$node->title</a><br>";
                }
        }
    ?>
    

    另外,请记住使用 Drupal 7 函数,例如 l() 和 t()。

    代替:

    echo "<a href='$targetPath'>$node->title</a><br>";
    

    写:

    echo l($node->title, $targetPath)."<br>";
    

    你会感到惊讶。 :)

    文档:

    https://api.drupal.org/api/drupal/includes%21common.inc/function/l/7 https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/t/7

    【讨论】:

    • 感谢您的建议。看起来很棒!但我不明白为什么你会有两个单独的实体 id..?我正在将两个字段与同一个用户 ID 进行比较...
    • 我相信您需要一个“或”。在这里检查:drupal.stackexchange.com/questions/14499/…您可能会更好地分别加载基于两个字段的用户,合并他们的项目ID并一次性加载。
    猜你喜欢
    • 2019-01-03
    • 2022-01-09
    • 1970-01-01
    • 2018-02-04
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    相关资源
    最近更新 更多