gpt4 book ai didi

Drupal 对数据库执行查询

转载 作者:行者123 更新时间:2023-12-04 06:46:20 26 4
gpt4 key购买 nike

我希望从我的 drupal 数据库中检索一些 nid。我有一个想要运行的查询。

  SELECT node.nid AS projectnid
FROM node node
INNER JOIN content_type_project node_data_field_project_client ON node.vid = node_data_field_project_client.vid
WHERE node_data_field_project_client.field_project_client_nid = (SELECT node_data_field_profile_company.field_profile_company_nid AS company_nid
FROM node node LEFT JOIN content_type_profile node_data_field_profile_company ON node.vid = node_data_field_profile_company.vid WHERE node.nid = 218)

我正在使用以下方法调用查询:
$query =
"
SELECT node.nid AS projectnid
FROM node node
INNER JOIN content_type_project node_data_field_project_client ON node.vid = node_data_field_project_client.vid
WHERE node_data_field_project_client.field_project_client_nid = (SELECT node_data_field_profile_company.field_profile_company_nid AS company_nid
FROM node node LEFT JOIN content_type_profile node_data_field_profile_company ON node.vid = node_data_field_profile_company.vid WHERE node.nid = 218)
";
$result = db_query($query);
dsm($result);

dsm 给了我一个空对象。当我直接运行 SQL 时,我会得到一个结果。

所以我的问题是如何让 db_query 将所有结果作为对象返回(我真的不介意是对象还是数组)。

(该 SQL 是通过查看 View 的查询输出创建的。)

这是对问题的跟进: Drupal Views Relationships and Arguments

I have a Person content type. It has a node reference field of a company which is also a content type. I then have a content type called Project. A project has a node reference to a company content type. I want to list all the projects related to a person id (nid)id (nid)

最佳答案

以下工作:

$query =
"
SELECT node.nid AS projectnid
FROM node node
INNER JOIN content_type_project node_data_field_project_client ON node.vid = node_data_field_project_client.vid
WHERE node_data_field_project_client.field_project_client_nid = (SELECT node_data_field_profile_company.field_profile_company_nid AS company_nid
FROM node node LEFT JOIN content_type_profile node_data_field_profile_company ON node.vid = node_data_field_profile_company.vid WHERE node.nid = 218)
";
$results = db_query($query);
while ($result = db_result($results)) {
dsm($result);
}

您需要使用 db_result() 来获取结果。使用 http://drupal.org/node/259432#comment-846946 解决了这个问题

关于Drupal 对数据库执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3727201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com