我做错了什么。我似乎无法根据drupal 7中的内容类型字段进行提取。
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字段中的条件拉出一个节点数组。任何帮助将不胜感激。
答案 0 :(得分: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.
}