php - 这些使用 db_or() 的 Drupal SQL 查询有什么问题?

标签 php mysql sql drupal drupal-7

我工作的机构在我们的 Drupal CMS 和 LDAP 服务器之间有一个现有且有效的集成。该集成的一项功能是能够从 LDAP 中提取记录来填充 Drupal 中的字段。我们告诉 Drupal 使用自定义模块填充哪些字段和内容类型,这些模块最初设计为仅容纳 (1) 内容类型的单个计算机名称和 (2) 每个字段一个单个计算机名称。

我们现在正在尝试修改这些配置项,使其以逗号分隔的列表形式存在,以便我们可以向任意数量的内容类型提供 LDAP 信息,这些内容类型可能不一定使用共享字段。对 (2) 的修改(即为一个字段输入多个机器名称并仍然填充所需数据的能力)已经开始工作。然而,完成 (1) 所需的修改(这将允许记录输出到多种内容类型)取决于 SQL 查询,我相信我的语法一定是错误的。

存储内容类型的计算机名称的配置项在内部称为 dart_netid_content_type由 Drupal 提供。这是旧代码,适用于标识单个内容类型的单个计算机名称:

// All nodes of type from dart_netid_content_type, with either no LDAP lookup time, or over 24 hours ago
$query = db_select('node');
$query->leftjoin('dart_ldap_update', 'ldap', 'node.nid = ldap.nid');
$query->fields('node', array('nid'))
  ->fields('ldap', array('nid', 'ldap_updated'))
  ->condition('type', variable_get('dart_netid_content_type', ''), '=')
  ->condition('status', '1', '=')
  ->condition(db_or()->condition('ldap_updated', NULL, 'IS')->condition('ldap_updated', $limit, '<'))
  ->orderby('ldap_updated', 'ASC');

这是新的查询,它应该适用于多种内容类型。有趣的是,查询并没有完全被破坏;如果您输入标识单个内容类型的单个计算机名称,则查询将继续运行。但是,如果您将逗号分隔的列表存储在 dart_netid_content_type 内,它已经在处理其他配置项,例如应该填充数据的字段的机器名称,那么查询将失败,并且模块将不会识别任何节点或填充其任何字段。

/*
 * Break up dart_netid_content_type into an array
 */
$dart_netid_content_type_array = explode(",", variable_get('dart_netid_content_type', ''));

/*
 * For each item in the array, create an 'or' conditional in a db_query that
 * checks for all of those content types
 */
$content_type_or = db_or();
foreach($dart_netid_content_type_array as $content_type) {
  $content_type_or->condition('type', $content_type, '=');
}

// All nodes of type from dart_netid_content_type, with either no LDAP lookup time, or over 24 hours ago
$query = db_select('node');
$query->leftjoin('dart_ldap_update', 'ldap', 'node.nid = ldap.nid');
$query->fields('node', array('nid'))
  ->fields('ldap', array('nid', 'ldap_updated'))
  ->condition($content_type_or)
  ->condition('status', '1', '=')
  ->condition(db_or()->condition('ldap_updated', NULL, 'IS')->condition('ldap_updated', $limit, '<'))
  ->orderby('ldap_updated', 'ASC');

以一种希望更直观的方式阐明这两个版本之间的预期差异:dart_netid_content_type现在可能是一种或多种内容类型。上一个查询正在搜索类似于 WHERE type = dart_netid_content_type 的内容。我现在试图哄它做的是查看 dart_netid_content_type_array 中的各个项目。并进行类似于 WHERE (type = dart_netid_content_type_array[0]) OR (type = dart_netid_content_type_array[1]) OR ... OR (type = dart_netid_content_type_array[sizeof(dart_netid_content_type_array)]) 的查询.

将其包装在 Drupal 的 db_query 和 db_or 语法中时,我的逻辑似乎有问题。

如果需要,我可以通过共享更多驱动 LDAP 到节点功能的自定义模块来提供更多上下文。

预先感谢您的帮助。

最佳答案

如何使用 IN 来确定内容类型是否在值数组中?像这样的事情:

$dart_ct_array = explode(",", variable_get('dart_netid_content_type', ''));
$dart_netid_content_types = "('" . implode("','", $dart_ct_array) . "')";

$query = db_select('node');
$query->leftjoin('dart_ldap_update', 'ldap', 'node.nid = ldap.nid');
$query->fields('node', array('nid'))
  ->fields('ldap', array('nid', 'ldap_updated'))
  ->condition('type', $dart_netid_content_types, 'IN')
  ->condition('status', '1', '=')
  ->condition(db_or()->condition('ldap_updated', NULL, 'IS')->condition('ldap_updated', $limit, '<'))
  ->orderby('ldap_updated', 'ASC');

关于php - 这些使用 db_or() 的 Drupal SQL 查询有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33900300/

相关文章:

python - 在 Python 或 SQL 中像求解器一样使用 Excel

mysql - 查询序列号

php - 使用 jscript 显示/隐藏 div

php - 将 mysql 从 5.1 更新到 5.6,但 php 信息仍然显示 5.1 客户端版本

php - codeigniter 2.0.3- fatal error

mysql - 关键字 - 和/

php - 从 utf8_unicode_ci Mysql 表打印文本时出现错误字符

javascript - 是否有可能利用网络技术构建家庭自动化系统?

mysql - 如何使用 CAMEL 批量处理 SQL 查询的结果?

php - 如何使用 PHP 和 MySQL 将 URL 类别编号更改为单词?