2011-12-29 103 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. 
}