2011-12-29 100 views
0

我做錯了什麼。我似乎無法在Drupal 7Drupal 7拉內容類型字段與db api

function ycs_list($number) { 
    $query = db_select('field_data_field_active_image', 'a') 
    ->fields('a', array('field_active_image_value', 'entity_id')) 
    ->condition('a.field_active_image_value', 1); 
    $query->join('node', 'n', 'n.nid = a.entity_id'); 
    $query 
    ->fields('n', array('nid', 'title', 'uid')) 
    ->range(0, $number) 
    ->addTag('node_access') 
    ->execute(); 
    print $query; 
    return $query; 
} 

拉基於內容類型的字段這是怎麼查詢打印:

SELECT a.field_active_image_value AS field_active_image_value, a.entity_id AS entity_id, n.nid AS nid, n.title AS title, n.uid AS uid FROM {field_data_field_active_image} a INNER JOIN {node} n ON n.nid = a.entity_id WHERE (a.field_active_image_value = :db_condition_placeholder_0) LIMIT 3 OFFSET 0 

它看起來權利,並在MySQL直接工作。我必須將db_conditon_placehoder_0更改爲1,它可以執行直接的sql查詢。我想根據active_image字段中的條件來拉一個節點數組。任何幫助將非常感激。

回答

1

不知道實際的錯誤你得到它的有點棘手調試現有的代碼,但我會用EntityFieldQuery class要做到這一點,而不是提醒:

function ycs_list($number) { 
    $query = new EntityFieldQuery; 
    $query->entityCondition('entity_type', 'node') 
    ->entityCondition('bundle', 'name_of_node_type') // Remove this line if you don't want to specify a particular node type for the query. 
    ->fieldCondition('field_active_image', 'value', 1) 
    ->range(0, $number) 
    ->addTag('node_access'); 

    // Execute the query and check for results. 
    $results = $query->execute(); 
    $nodes = array(); 
    if ($results && isset($results['node'])) { 
    // Load the node objects based on the node ids returned from the query. 
    $nodes = node_load_multiple(array_keys($results['node'])); 
    } 

    // If nodes matching your conditions were found you now have an array of fully loaded node objects in $nodes. 
}