函数用法:
读取content type叫'slider_show'的所有节点
$type = 'slider_show';//你的类型
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')//读取node
->propertyCondition('type', $type)//你的类型
->propertyCondition('status', 1)
->fieldCondition('status', 1)
//->propertyOrderBy('created', 'DESC')
->addTag('random')//The easiest way for to order randomly is to add a tag and do an alter on it
->range(0,50000)
->execute();
//debug($result);
//取得的是一个含nid的数组。然后用node_load读出全部node
$nids = array_keys($result['node']);
$nodes = node_load_multiple($nids);
//到这里就读出了所有的node,可以使用foreach来循环处理了
debug($nodes);
参数说明(参考地址drupal官网API:https://drupal.org/node/1343708):
->entityCondition($name, $value, $operator = NULL)
entityConditions are conditions most types of entity classes should support and are in DrupalDefaultEntityController class.
- entity_type
- value examples: 'node', 'taxonomy_term', 'comment', 'user', 'file'
- operator examples: operator disregarded
$query->entityCondition('entity_type', 'node')
- bundle. (not supported in comment entity types)
- value examples: 'article', 'page'.
- operator examples: '=' for strings and 'IN' for arrays.
$query->entityCondition('bundle', 'article')
- revisions_id.
- value examples: 3,4,52,342
- operator examples: =, <, >, != for integer values and 'IN' and 'NOT IN' for arrays of values.
$query->entityCondition('revisions_id', $revision_id, '=')
- entity_id. e.g. node id
- value examples: 3,4,52,342
- operator examples: =, <, >, != for integer values and 'IN' and 'NOT IN' for arrays of values.
$query->entityCondition('entity_id', array(17, 21,422), 'IN')
->propertyCondition($name, $value, $operator = NULL)
These conditions are specific to each entity implementation such as node, user, comment, etc. and generally map to the columns in the database where the entity itself is stored. e.g. node, users, comment, file_managed, taxonomy_term_data, etc tables. A grep/search for "Implements hook_entity_info()" will show code indicating the name of the entity's base table.
- status (nodes). e.g. propertyCondition('status', 1)
- type (nodes). e.g. ->propertyCondition('type', array('article', 'page', 'blog'))
- uid (nodes) .e.g. ->propertyCondition('uid', $uid)
- uri (media) .e.g. ->propertyCondition('uri', '%.jpg', 'LIKE')
- type (media) e.g. ->propertyCondition('type', 'image');
->fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL)
These conditions are specific to a field implementation.
Looking at the body field in the article node type, a field condition would look like this:
->fieldCondition('body', 'value', 'A', 'STARTS_WITH')
- field name. Though the field table in the database is named 'field_data_body', the actual field name is 'body'. This is in the field_config_instance table.
- column. This is the column in the database that should be matched, with field name prefix removed. For body field, the database columns are: body_value, body_summary, body_format, and language. 'value', 'summary', 'format', and 'language' are the actual arguments you would use.
Likewise an image field would use 'fid', 'alt', and 'title' as column names; an entity reference field would use 'target_id' and 'target_type' as column names.
->propertyOrderBy($column, $direction = 'ASC')
does not work on all properties.
->range($start = NULL, $length = NULL)
Restricts a query to a given range in the result set.
->count()
Sets the query to be a count query only, i.e.
$count = $query->count()->execute();
->addMetaData($key, $object)
Adds additional metadata to the query. One important usage of this method is to run the query as another user, since EntityFieldQuery's fieldCondition will take current user's permission into account, this is not always the desired result, to run the query as for example user 1, use the following code:
$query->addMetaData('account', user_load(1));
Operators
The $value and $operator parameters of entityCondition, propertyCondition, and fieldCondition behave the same. The $operator values depend on the $value format as summarized in http://api.drupal.org/api/drupal/includes--entity.inc/function/EntityFie...
Selected Resources
Code Examples
- Tests for EntityFieldQuery are in core in modules\simpletest\tests\entity_query.test
- grep/search drupal code base for "new EntityFieldQuery()" in core and contrib will find additional examples
-------------------------------------------------------------------------------------------
读取taxonomy terms
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'taxonomy_term')//读取taxonomy term
->propertyCondition('vid', 3)//你要读取的类型,可以改换成其他数字
//->propertyCondition('type', $type)
//->propertyCondition('status', 1)
->propertyOrderBy('weight', 'ASC')
->range(0,50000)
->execute();
//debug($result);
foreach($taxonomyTerms['taxonomy_term'] as $term) {
$relevantTerms[] = $term->tid;
}
// $relevantTerms will now have the terms of your target vocabulary
-------------------------------------------------------------------------------------------
//get all entity type
//debug(entity_get_info());
-------------------------------------------------------------------------------------------
//get all taxonomy terms
debug(taxonomy_get_tree(3));