使用实体字段查询搜索多个字段的最佳方法?

时间:2016-03-23 22:24:48

标签: php mysql database drupal

使用实体字段查询搜索多个字段的最佳方法是什么?我有一个名为“Projects”的内容类型,它包含“Project Manager”和“Developer”的自定义字段,它们是对用户的引用,我试图在每个用户的配置文件中显示给定用户关联的项目列表。< / p>

这是我到目前为止所做的:

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

?>

1 个答案:

答案 0 :(得分:0)

您似乎需要根据[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()&amp; 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