简体   繁体   中英

Sql query in drupal module - what is node?

I am running the following query which returns an array of stcClass objects as below:

 $result = db_query('SELECT node.title AS node_title, 
                   node.nid AS nid, 
                   node.created AS node_created, 
                   \'node\' AS field_data_my_field_node_entity_type 
                   FROM {node} node WHERE (( (node.status = :status) 
                   AND (node.type IN  (:type)) )) 
                   ORDER BY node_created DESC', 
                   array(':status'=>'1', ':type'=>'_my_content_type'));


stdClass Object
(
[node_title] => my sample title
[nid] => 331
[node_created] => 1367500781
[field_data_my_field_node_entity_type] => node
)

It returns an array of stdClass objects. My problem is that in the field 'field_data_my_field_node_entity_type' I have only the string 'node' because this is passed into the query as a string. I don't really know much about this, but I suppose I can get a value there if I have the correct syntax.

Any idea how this query is supposed to work?

Thanks in advance

You are getting that value because you are setting it in the SQL query with 'node' AS field_data_my_field_node_entity_type .

If your question is how to get the entity type for a node, that is always node ; there isn't a field in the "nodes" table that contains that value.

If you added a "field_data_my_field_node_entity_type" field to one or more content types, and you want to get the content of that field, you need to follow these steps, since the content of node fields is not contained in the "node" table.

  • Get the node ID of the nodes
  • Load the nodes with node_load()
$result = db_query('SELECT nid FROM {node} WHERE ((status = :status) AND (type IN  (:type)) ORDER BY created DESC', array(':status'=>'1', ':type'=>'_my_content_type'))->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
  $node = node_load($row['nid']);
  // Access the field as $node->field_data_my_field_node_entity_type.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM