dqd22496 2016-03-23 22:24
浏览 15

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

What's the best way to use Entity Field Query to search multiple fields? I have a content type called "Projects" that has custom fields for "Project Manager" and "Developer, which are references to users, and I am trying to display a list of projects a given user is associated with on each user's profile.

This is what I have so far:

<?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条回答 默认 最新

  • dougou8552 2016-03-24 20:32
    关注

    It seems that you need to load users based on [field_1] OR [field_2] values.

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

    Also, please remember to use Drupal 7 functions like l() & t().

    Instead of:

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

    Write:

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

    You will be surprised. :)

    Docs:

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

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分