EntityFieldQuery 쿼리 조건을 사용할 때 빈 (Null) 필드 제외


31

xyz 필드가 비어있는 모든 엔티티를 선택할 수 있습니까?

나는 다음과 같은 진정을 시도했다.

->fieldCondition('field_name', 'value', NULL, 'IS NOT NULL');

그러나 이것은 작동하지 않는 것 같습니다.

어떤 아이디어?

답변:


19

당신이에 보면 fieldCondition의 문서 페이지에 다음과 같은 경고가 표시됩니다

이 방법을 사용할 때 빈 필드 값을 가진 엔티티는 EntityFieldQuery 결과에서 제외됩니다.

필드가 존재하는지 확인하면 Drupal 8의 entityFieldQuery에 추가되었지만 불행히도 Drupal 7로 백 포트되지는 않습니다 .

이를 달성하기위한 다양한 방법이 있습니다.

  1. @Clive에서 언급 한대로 tag 및 hook_query_TAG_alter를 사용 하여 Drupal 문제에 대한 주석 4를 참조하십시오 .
  2. 먼저 NULL이 아닌 모든 항목을 쿼리 한 다음 @seddonym의 답변과 Drupal 문제에 대한 주석 5에 설명 된대로 이전 항목을 제외한 모든 항목을 쿼리하십시오 .
  3. EntityfieldQuery 보다 SelectQuery rathen을 사용하여 쿼리를 작성할 수 있습니다 .

_

$q = db_select('node', 'n');
$q->fields('n', array('type'))
  ->condition('n.type', 'my_node_type', '=')
  ->addJoin('LEFT', 'field_data_field_my_field', 'f', 'f.entity_id = n.nid');
$q->isNull('f.value');
$r = $q->execute();

15

을 사용할 수 != NULL있지만 = NULL어떤 이유로 든 사용할 수 없습니다 .

이것은 내 해결 방법입니다.

  //Get all the entities that DO have values
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'MY_TYPE')
    ->fieldCondition('field_MY_FIELD', 'value', 'NULL', '!=');
  $result = $query->execute();

  if (is_array(@$result['registration'])) {
    //Now get all the other entities, that aren't in the list you just retrieved
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'MY_TYPE')
      ->entityCondition('entity_id', array_keys($result['MY_TYPE']), 'NOT IN');
    $result_two = $query->execute(); 
  }

10

문서에 따르면 null과 isull을 사용할 수 있습니다. 그것을 쓰는 구체적인 방법이 있습니다.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'article')
  ->propertyCondition('status', 1)
  ->fieldCondition('field_news_types', 'value', 'spotlight', '=')
  ->fieldCondition('field_photo', 'fid', 'NULL', '!=')
  ->fieldCondition('field_faculty_tag', 'tid', $value)
  ->fieldCondition('field_news_publishdate', 'value', $year. '%', 'like')
  ->range(0, 10)
  ->addMetaData('account', user_load(1)); // run the query as user 1

$result = $query->execute();

if (isset($result['node'])) {
  $news_items_nids = array_keys($result['node']);
  $news_items = entity_load('node', $news_items_nids);
}

9

짧은 대답은 직접 할 수 없다는 것입니다 ( EntityFieldQuery는 isNull 또는 isNotNull을 지원하지 않음 참조 ). 내가 올바르게 기억한다면 이것은 테이블을 조인하기 위해 s EntityFieldQuery만 사용 한다는 사실의 부작용 INNER JOIN입니다.

그러나 hook_query_TAG_alter()에 태그를 사용 하고 추가 하는 해결 방법이 있지만 EntityFieldQuery위에 링크 된 페이지의 마지막 주석에 예가 있습니다.


5

Drupal 7 에서 여기에 제안 된 다음 해결 방법을 확인 하십시오 .

쿼리 인스턴스를 변경하려면 태그를 등록하십시오.

<?php
/**
 * Implements hook_query_TAG_alter()
 */
function MYMODULE_query_node_is_not_tagged_alter(QueryAlterableInterface $query) {
  $query->leftJoin('field_data_field_tags', 'o', 'node.nid = o.entity_id AND o.entity_type = :entity_type');
  $query->isNull('o.field_tags_tid');
}
?>

Obs .:이 쿼리 태그 alter는 "node"엔티티 유형에서만 작동합니다. "태그"어휘와 관련된 "field_tags"를 혼동하지 마십시오. "카테고리"와 같은 다른 단어가 될 수 있습니다.

아직 EntityFieldQuery를 사용하여 태그가 지정되지 않은 모든 노드를 가져 오려면 addTag () 메소드를보십시오.

<?php
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'news')
  ->addTag('node_is_not_tagged')
  ->propertyCondition('status', 1);
$result = $query->execute();
?>

다른 예 :

  $result = $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'my_content_type')
    ->fieldCondition('field_mine_one', 'value', '', '<>')
    ->fieldCondition('field_mine_two', 'value', '', '<>')
    ->addTag('my_custom_tag')
    ->deleted(FALSE)
    ->propertyOrderBy('changed', 'DESC')
    ->range(0, $my_range_value)
    ->execute();

그런 다음 나 만 설정 한 hook_query_TAG_alter사실을 활용하여 구현 my_custom_tag했습니다.

/**
 * Implements hook_query_TAG_alter()
 */
function MYMODULE_query_TAG_alter(QueryAlterableInterface $query) {
  $query->leftJoin('field_data_field_other', 'o', 'node.nid = o.entity_id');
  $query->isNull('o.field_other_value');
}

또 다른 예:

<?php
  //Get all the entities that DO have values
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'MY_TYPE')
    ->fieldCondition('field_MY_FIELD', 'value', 'NULL', '!=');
  $result = $query->execute();

  if (is_array(@$result['registration'])) {
    //Now get all the other entities, that aren't in the list you just retrieved 
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'MY_TYPE')
      ->entityCondition('entity_id', array_keys($result['MY_TYPE']), 'NOT IN');
    $result_two = $query->execute();  
  }
?>

아래의 더 완전한 예제는 분류 용어 용어를 비우고 일부 변경 사항을 적용하는 cron 태스크에 여러 노드를로드합니다.

/**
 * Implements hook_cron().
 */
function MYMODULE_cron() {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'property')
    ->propertyOrderBy('changed', 'DESC')
    ->addTag('type_is_null')
    ->range(0,50); // Maximum of 50.
  $result = $query->execute();

  if (!empty($result['node'])) {
    $nids = array_keys($result['node']);
    $nodes = node_load_multiple($nids);

    foreach ($nodes as $node) {
      // do_some_stuff($node);
    }
  }
}

/**
 * Implements hook_query_TAG_alter()
 */
function MYMODULE_query_type_is_null_alter(QueryAlterableInterface $query) {
  $query->leftJoin('field_data_field_foo', 'f', 'node.nid = f.entity_id AND f.entity_type = :entity_type');
  $query->isNull('f.field_foo_tid'); // Check name by SQL: DESC field_data_field_foo

  $query->leftJoin('field_data_field_bar', 'b', 'node.nid = b.entity_id AND b.entity_type = :entity_type');
  $query->isNull('b.field_bar_tid'); // Check name by SQL: DESC field_data_field_bar
}

3

널을 따옴표로 묶어야합니다.

->fieldCondition('field_name', 'value', 'NULL', '!=');

2

내가 틀렸다면 정정 해주세요. 그것은 단순히해야 할 것 같습니다

$query->fieldCondition('field_name');

field_name필드 가 비어있는 모든 노드를 제외하려면 o_O

Drupal에서 테스트되었습니다 version >= 7.43.


실제로 작동합니다. 그들은 더 많은 답을 얻었습니다.
Joren
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.