EntityFieldQuery 多个可选条件
在一定条件下是否有更优化、更短的方式获取节点?
$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', $node_type)
->propertyCondition('title', $title)
->fieldCondition('field_number', 'value', '1', '=');
->propertyCondition('status', 1, '=')
->execute();
// $result['node'] contains a list of nids where the title matches
if (!empty($result['node']) {
// You could use node_load_multiple() instead of entity_load() for nodes
$nodes = entity_load('node', array_keys($result['node']));
}
$query_two = new EntityFieldQuery;
$result_two = $query_two
->entityCondition('entity_type', 'node')
->propertyCondition('type', $node_type)
->propertyCondition('title', $title)
->fieldCondition('field_number', 'value', '2', '=');
->propertyCondition('status', 1, '=')
->execute();
// $result_two['node'] contains a list of nids where the title matches
if (!empty($result_two['node']) {
// You could use node_load_multiple() instead of entity_load() for nodes
$nodes_two = entity_load('node', array_keys($result_two['node']));
}
Is there more optimal and shorter way to get nodes with certain conditions?
$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', $node_type)
->propertyCondition('title', $title)
->fieldCondition('field_number', 'value', '1', '=');
->propertyCondition('status', 1, '=')
->execute();
// $result['node'] contains a list of nids where the title matches
if (!empty($result['node']) {
// You could use node_load_multiple() instead of entity_load() for nodes
$nodes = entity_load('node', array_keys($result['node']));
}
$query_two = new EntityFieldQuery;
$result_two = $query_two
->entityCondition('entity_type', 'node')
->propertyCondition('type', $node_type)
->propertyCondition('title', $title)
->fieldCondition('field_number', 'value', '2', '=');
->propertyCondition('status', 1, '=')
->execute();
// $result_two['node'] contains a list of nids where the title matches
if (!empty($result_two['node']) {
// You could use node_load_multiple() instead of entity_load() for nodes
$nodes_two = entity_load('node', array_keys($result_two['node']));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您当然可以使用
->fieldCondition('field_number', 'value', array(1, 2))
,但除此之外,我不知道(我写了EntityFieldQuery
)。即使您将其重写为仅 SQL 存储查询,也不会简单多少。您不需要指定
=
作为运算符,也不需要指定IN
,它们是标量/数组值的默认值。Well, you certainly could use
->fieldCondition('field_number', 'value', array(1, 2))
, but other than that, not that I know of (and I wroteEntityFieldQuery
). Even if you were to rewrite this to an SQL-storage only query, it would not be much simpler.You do not need to specify
=
as the operator and you do not need to specifyIN
either, they are defaults for a scalar / array value.我最近为 EntityFieldQuery 编写了一个包装器,因为我使用它太多了。那么我调用它的方式就变成了,结果可以是一个 id 或一个 id 数组。希望这是有道理的。
I recently wrote a wrapper for EntityFieldQuery, since i just use it too much. Then the way I call it becomes to, the result can be an id or an array of ids. Hope this makes sense.