Drupal 7 EntityFieldQuery多个可选条件
在某些条件下,是否有更优化、更短的方法来获取节点Drupal 7 EntityFieldQuery多个可选条件,drupal-7,Drupal 7,在某些条件下,是否有更优化、更短的方法来获取节点 $query = new EntityFieldQuery; $result = $query ->entityCondition('entity_type', 'node') ->propertyCondition('type', $node_type) ->propertyCondition('title', $title) ->fieldCondition('field_number', 'value
$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']));
}
嗯,你当然可以用,但除此之外,据我所知(我也写过)。即使将其重写为仅SQL存储查询,也不会简单得多
您不需要将
=
指定为运算符,也不需要在中指定,它们是标量/数组值的默认值。我最近为EntityFieldQuery编写了一个包装器,因为我用得太多了。然后我调用它的方式变成,结果可以是一个id或一个id数组。希望这是有意义的
$ids = qplot_api_find_nodes2(
array(
'type' => 'content',
'field_content_project' => array('target_id', 10, '='),
),
array(
'created' => 'ASC'
),
TRUE
);
/**
* Returns node nid(s) with filters and sorts.
*
* @param array $conds
* Condition entries, there're three different type of conditions
* 1. prefixed with entity_, ex. 'entity_type' => 'node'
* 2. prefixed with field_, ex. 'field_project',
* two formats allowed, simple version 'field_tag' => 'abc',
* or long version, 'field_tag' => array('target_id', 11, '=')
* 3. no prefix or other prefix, 'title' => 'abc'
* Default $conds contains 'entity_type' => 'node' entry.
*
* @param array $sorts
* Sort entiries, there're two different type of sorts
* 1. prefixed with field_, ex. 'field_tag' => array('target_id', 'ASC')
* 2. no prefix or other prefix, 'title' => 'ASC'
* Default $sorts are empty
*
* @param bool $all
* If all matching nid are returned, or just the first one, default FALSE
*
* @return int
* The nid for the supplied id or 0 if not found.
* Or array of nids if $all = TRUE
*
* @author Fang Jin <windmaomao@gmail.com>
*/
function qplot_api_find_nodes2($conds, $sorts = NULL, $all = FALSE) {
$conds = array_merge(array('entity_type' => 'node'), $conds);
if (empty($sorts)) {
$sorts = array();
}
$query = new EntityFieldQuery();
// apply condition to query
foreach ($conds as $key => $value) {
$splits = explode('_', $key);
$type = $splits[0];
if (count($splits) == 1) {
$type = 'property';
}
switch ($type) {
case 'entity':
$query->entityCondition($key, $value);
break;
case 'field':
if (is_array($value)) {
$property = isset($value[1]) ? $value[0] : 'value';
$assigned = isset($value[1]) ? $value[1] : $value[0];
$operator = isset($value[2]) ? $value[2] : '=';
$query->fieldCondition($key, $property, $assigned, $operator);
} else {
$query->fieldCondition($key, 'value', $value);
}
break;
// rest of them are all property
default:
$query->propertyCondition($key, $value);
break;
}
}
// apply sort to query
foreach ($sorts as $key => $value) {
$splits = explode('_', $key);
$type = $splits[0];
if (count($splits) == 1) {
$type = 'property';
}
switch ($type) {
case 'field':
$query->fieldOrderBy($key, $value[0], $value[1]);
break;
default:
$query->propertyOrderBy($key, $value);
break;
}
}
$result = $query->execute();
$ctype = $conds['entity_type'];
if (!empty($result[$ctype])) {
$keys = array_keys($result[$ctype]);
if ($all) {
return $keys;
}
else {
return $keys[0];
}
} else {
if ($all) {
return array();
} else {
return 0;
}
}
}
$ids=qplot\u api\u find\u节点2(
排列(
'类型'=>'内容',
'field_content_project'=>array('target_id',10',='),
),
排列(
“已创建”=>“ASC”
),
真的
);
/**
*返回带有筛选器和排序的节点nid。
*
*@param数组$conds
*条件条目,有三种不同类型的条件
* 1. 前缀为实体,例如“实体类型”=>“节点”
* 2. 前缀为字段,例如“字段项目”,
*允许两种格式,简单版本“field_tag”=>“abc”,
*或长版本,'field_tag'=>array('target_id',11',=')
* 3. 没有前缀或其他前缀,'title'=>'abc'
*默认$conds包含“实体类型”=>“节点”条目。
*
*@param数组$sorts
*排序实体,有两种不同类型的排序
* 1. 前缀为field,例如“field\u tag”=>array('target\u id','ASC')
* 2. 没有前缀或其他前缀,'title'=>'ASC'
*默认$sorts为空
*
*@param bool$all
*如果返回所有匹配的nid,或仅返回第一个nid,则默认为FALSE
*
*@return int
*所提供id的nid或0(如果未找到)。
*或NID数组,如果$all=TRUE
*
*@作者方进
*/
函数qplot\u api\u find\u nodes2($conds,$sorts=NULL,$all=FALSE){
$conds=array\u merge(数组('entity\u type'=>'node'),$conds);
if(空($sorts)){
$sorts=array();
}
$query=new EntityFieldQuery();
//将条件应用于查询
foreach($key=>$value){
$splits=分解(“u',$key);
$type=$splits[0];
如果(计数($splits)==1){
$type='property';
}
交换机($类型){
案例“实体”:
$query->entityCondition($key,$value);
打破
案例“字段”:
if(是_数组($value)){
$property=isset($value[1])?$value[0]:'value';
$assigned=isset($value[1])?$value[1]:$value[0];
$operator=isset($value[2])?$value[2]:“=”;
$query->fieldCondition($key、$property、$assigned、$operator);
}否则{
$query->fieldCondition($key,'value',$value);
}
打破
//其余的都是财产
违约:
$query->propertyCondition($key,$value);
打破
}
}
//对查询应用排序
foreach($key=>$value排序){
$splits=分解(“u',$key);
$type=$splits[0];
如果(计数($splits)==1){
$type='property';
}
交换机($类型){
案例“字段”:
$query->fieldOrderBy($key、$value[0]、$value[1]);
打破
违约:
$query->propertyOrderBy($key,$value);
打破
}
}
$result=$query->execute();
$ctype=$conds['entity_type'];
如果(!empty($result[$ctype])){
$keys=数组_keys($result[$ctype]);
如果(全部){
返回$keys;
}
否则{
返回$keys[0];
}
}否则{
如果(全部){
返回数组();
}否则{
返回0;
}
}
}
感谢您抽出时间回答此问题,并感谢EFQ,这是一项很棒的功能。