sql query for drupal 7 module
This query I pulled and worked with from a view.
(it is meant to be handed a node id, and return all nodes that have this node id as a node reference)
function jsonview_node_update($node){
if ($node->type == "company") {
$query = "SELECT DISTINCT node.title AS node_title, node.nid AS nid
FROM {node} node
LEFT JOIN field_data_field_campaigncompany ON node.nid = field_data_field_campaigncompany.entity_id AND (field_data_field_campaigncompany.entity_type = node AND field_data_field_campaigncompany.deleted = 0)
WHERE (( (node.status = '1') AND (field_data_field_campaigncompany.f开发者_如何学Pythonield_campaigncompany_nid IN ('".$node->nid."')) AND (node.type IN ('campaign')) ))";
$result = db_query($query);
file_put_contents('/tmp/ref.txt',json_encode($result));
}
is returning
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node' in 'on clause': SELECT DISTINCT node.title AS node_title, node.nid AS nid FROM {node} node LEFT JOIN field_data_field_campaigncompany ON node.nid = field_data_field_campaigncompany.entity_id AND (field_data_field_campaigncompany.entity_type = node AND field_data_field_campaigncompany.deleted = 0) WHERE (( (node.status = '1') AND (field_data_field_campaigncompany.field_campaigncompany_nid IN ('14')) AND (node.type IN ('campaign')) )); Array ( ) in jsonview_node_update() (line 224 of /jsonview.module).
Not sure what is wrong here.. I've tried out relation and a couple other modules and none really do the trick
(Answered in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
solved:
function jsonview_node_update($node){
if ($node->type == "company") {
$query = "SELECT DISTINCT title, nid
FROM {node} node
LEFT JOIN field_data_field_campaigncompany ON node.nid = field_data_field_campaigncompany.entity_id AND (field_data_field_campaigncompany.entity_type = 'node' AND field_data_field_campaigncompany.deleted = 0)
WHERE (( (node.status = '1') AND (field_data_field_campaigncompany.field_campaigncompany_nid IN ('".$node->nid."')) AND (node.type IN ('campaign')) ))";
// $query = "SELECT nid, title FROM {node}";
$result = db_query($query);
// $result = db_query("SELECT nid, title FROM {node}");
foreach ($result as $record) {
file_put_contents('/tmp/ref.txt',json_encode($record),FILE_APPEND);
// Do something with each $record
}
}
}
精彩评论