2016-03-23 9 views
0

エンティティフィールドクエリを使用して複数のフィールドを検索する最適な方法は何ですか? 「Project Manager」と「Developer」のカスタムフィールドを持つ「Projects」というコンテンツタイプがあり、各ユーザーのプロファイルに関連付けられているプロジェクトの一覧を表示しようとしています。エンティティフィールドクエリで複数のフィールドを検索する最も良い方法は?

これは私がこれまで持っているものです。

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

?> 

答えて

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()& 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

+0

が提案していただきありがとうございます。..素晴らしいですね!しかし、私はあなたが2つの別個のエンティティIDを持つ理由を理解していません..?私は同じユーザーIDに対して2つのフィールドを比較しています... –

+0

私はあなたが 'OR'が必要と信じています。ここで確認してください:http://drupal.stackexchange.com/questions/14499/using-or-with-entityfieldquery 2つのフィールドに基づいてユーザーを別々に読み込み、プロジェクトIDをマージして一度にロードするほうが良いかもしれません。 –

関連する問題