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

这是我到目前为止的内容:

<?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>";
  }

?>

最佳答案

似乎您需要根据[field_1]或[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

关于php - 使用实体字段查询搜索多个字段的最佳方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36189856/

10-11 02:52