EntityFieldQuery 多个可选条件

发布于 2024-11-07 15:57:28 字数 1157 浏览 0 评论 0原文

在一定条件下是否有更优化、更短的方式获取节点?

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-11-14 15:57:28

好吧,您当然可以使用 ->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 wrote EntityFieldQuery). 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 specify IN either, they are defaults for a scalar / array value.

近箐 2024-11-14 15:57:28

我最近为 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 <[email protected]>
 */
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;
        }
    }
}

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.

$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 <[email protected]>
 */
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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文