EntityFieldQuery API

论坛: 


Drupal 7的推出EntityFieldQuery API。这个类是一种机制,用于快速构建实体和字段的查询 - 通常节点,但并不限于此。
Drupal7提供丰富的数据库抽象层。 EntityFieldQuery可以构建查询无需使用SQL语句。                          

Drupal内核本身大多采用EntityFieldQuery作为使用函数,在许多情况下,可以快速简单地查询输出一个特定的的字段值。 

开始构建查询

启动一个entity的查询是这样的:                            :

$query = new EntityFieldQuery();

是不是非常简单?。现在让我们来添加一些条件的查询。

$query
 ->entityCondition('entity_type', 'node')
 ->entityCondition('bundle', 'article')
 ->propertyCondition('status', 1)
 ->propertyOrderBy('created', 'DESC');

让我们来看看什么是怎么回事。首先,注意缺乏方法调用之间的分号。每个EntityFieldQuery方法返回相同的EntityFieldQuery
对象的方法被调用。这使我们能够链方法调用起来,从而加快了编码,并使其更容易阅读。这看起来很熟悉的人谁使用jQuery的。这可能很容易被写成像这
样:

$query->entityCondition('entity_type', 'node')->entityCondition('bundle', 'article')->propertyCondition('status', 1)->propertyOrderBy('created', 'DESC');

但是,这确实是不容易的在所有的阅读。我建议把每种方法在单独的一行。
现在让我们来看看每一种方法会发生什么

->entityCondition('entity_type', 'node')

第一种方法使用一个entitycondition告诉查询查找节点实体。它可能看起来像指定这是多余的但在Drupal 7许多其他的东西也用户实体分类方面你需要限制你的设置

->entityCondition('bundle', 'article')

第二种方法告诉查询的节点类型限制设置。这可以离开如果你想查询所有可用的节点,但在大多数情况下,你会寻找特定的节点类型。注意,第二个参数,文章在这种情况下,可以是一个字符串或一个数组的查询会自动调整。所以,如果你正在寻找这两篇文章和网页节点的类型,你可以改写部分内容如下:   

->entityCondition('bundle', array('article', 'page'))

正如你可以看到拓展您的查询非常容易.

->propertyCondition('status', 1)

第三种方法是一个propertycondition。在这种情况下,属性为给定的实体类型的基表中的任何列。在节点的情况下,这可能是它是否发表创建用户节点创建时间等。在上面的例子中我们修改我们的查询只返回.

->propertyOrderBy('created', 'DESC')

最后一种方法在上面的例子中使用propertyorderby设置命令。在这种情况下我们只是问要按顺序返回结果.



查询 Fields 和 Limiting

Now let’s add a query on a field value using a fieldCondition. Let’s assume each of the node types in our example has a field ‘field_us_state’ assigned to it. We’re going to find nodes that are associated with the New York Tri-state area, which also includes Connecticut and New Jersey. This would look like this:

$query->fieldCondition('field_us_state', 'value', array('CT', 'NJ', 'NY'));

We can order by field values as well, if that is useful to us. Note that ordering conditions are processed in the order that they are added to the query请注意,排序条件处理的顺序,将它们添加到查询.

让我们添加一个范围:限制查询最近10个项目:

$query->range(0,10);

Et Voilà

最后我们执行查询并分配给一个变量:

$result = $query->execute();


我们返回实体ID匹配指定的查询条件的数组. 如果我们查询 nodes, 信息将 $result['node']. 在大多数情况下 what you actually want are the nids that have been returned. 是很容易的:

$nids = array_keys($result['node']);

组合在一起,就是下面的代码:   

$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'article')
  ->propertyCondition('status', 1)
  ->propertyOrderBy('created', 'DESC')
  ->fieldCondition('field_us_state', 'value', array('CT', 'NJ', 'NY'))
  ->range(0,10);
$result = $query->execute();
$nids = array_keys($result['node']);

这是不是很多的代码在所有这样的结果。此外,我们不需要知道的关于SQL或甚至对底层的存储引擎使用的东西。我们只是写的查询在这个简单的语法,并entityfieldquery API并转化为一个查询,Drupal的数据库抽象层的理解和执行它为我们工作.

What would we do with this list of nids? 相当多的东西。我们可以加载它们首先:

$nodes = node_load_multiple($nids);

我们可能需要在一个teaser样式显示这10个节点.这也很容易做到。让我们产生一个Drupal渲染数组

$output = node_view_multiple($nodes);

More To Come

这仅仅是个开始。看这个帖子的2部分,我们将把entityfieldquery使用更具体的例子。还调查了其他的树岗位工程师解释更先进的相关entityfieldquery主题.


转载来源:http://www.phase2technology.com/blog/entityfieldquery-let-drupal-do-the-...

相关文章: