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

标签 php mysql database drupal

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

这是我到目前为止所拥有的:

<?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/

相关文章:

mysql - 低延迟如何不=高吞吐量?

database - 如果查询返回所有行,数据库索引有用吗?

php - 获取数据库的最后一个实体,增加它的值并创建新记录 PHP SQL

php - 购物车持久性 : $_SESSION or browser cookie?

php - WordPress - 无法在条件语句中创建分类法

php - 在 php 中显示图像

java - 坚持一对多关系时违反约束

c# - 在 asp.net 中使用散列密码创建登录名

php - 如何在magento中从productId获取子类别

python - 在 Python 中打开 Alteryx .yxdb 文件?