简体   繁体   English

创建动态Drupal节点期间的数据库查询以获取实体ID

[英]Database Query during Dynamic Drupal node creation to get Entity ID

I have a script which grabs the latest image from our surf webcam, it saves a node of type Photos. 我有一个脚本可以从我们的冲浪网络摄像头获取最新图像,并保存一个“照片”类型的节点。 In that content type i have an entity field which references the swell size (content type swell_data). 在该内容类型中,我有一个引用膨胀大小的实体字段(内容类型swell_data)。 I can add a static entity id no problem: 我可以添加一个静态实体id没问题:

// Reference Swell Data ID 176821 
$node->field_buoy_ref[$node->language][0]['target_id'] = 176821;

How would I dynamically find the latest node created by the swell_data content type (added hourly using a CRON job) and use that value instead? 我如何动态查找由swell_data内容类型(使用CRON作业每小时添加一次)创建的最新节点,并使用该值代替? For your reference this is the current script i am using (thanks to FooNinja ) 供您参考,这是我正在使用的当前脚本(感谢FooNinja

define('DRUPAL_ROOT', getcwd());
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$bodytext = "This was the view from the Camera at ".date('gA')." on ".date('l F jS Y');

$node = new stdClass(); // Create a new node object
$node->type = "photos"; // Or page, or whatever content type you like
node_object_prepare($node); 

$node->title = "Caswell Camera ". date('D M j G:i');
$node->language = LANGUAGE_NONE; 

// Author
$node->name = "Gower Live"; // UID of the author of the node; or use $node->name
// Publish Node
$node->status = 1;
//Set Body Text
$node->body[$node->language][0]['value']   = $bodytext;
$node->body[$node->language][0]['summary'] = text_summary($bodytext);
$node->body[$node->language][0]['format']  = 'filtered_html';

//Set Image Location (Caswell)
$node->field_photo_location[$node->language][0]['lat'] = 51.570195982718374;
$node->field_photo_location[$node->language][0]['lng'] = -4.030849980797484;

// Set the Photos date
$node->field_image_date[$node->language][0]['value'] = date("Y-m-d H:i:s");

// From the Webcam
$node->field_webcam_photo[$node->language][0]['value'] = "Caswell";

// Reference Wave Buoy
$node->field_buoy_ref[$node->language][0]['target_id'] = 176821; // I'd this value dynamically pulled from the DB

// Attach Latest Webcam Image
$file_path = drupal_realpath('images/webcams/caswell-webcam.jpg');
$file = (object) array(
            'uid' => 1,
            'uri' => $file_path,
            'filemime' => file_get_mimetype($file_path),
            'status' => 1,
);
// You can specify a subdirectory, e.g. public://foo/
$file = file_copy($file, 'public://');
$node->field_user_photo[$node->language][0] = (array) $file;

$path = 'node_created_on' . date('YmdHis');
$node->path = array('alias' => $path);

if($node = node_submit($node)) { // Prepare node for saving
    node_save($node);
    echo "Node with nid " . $node->nid . " saved!\n";
}

thanks 谢谢

Lee 背风处

I added the following below the Bootstrap Line and bingo. 我在引导线和宾果游戏下面添加了以下内容。

$result = db_query("SELECT nid, title, created FROM {node} WHERE type = :type AND title =:title", array(
  ':type' => 'wavebuoy_data',
  ':title' => "Buoy ". date('D M j gA'),
));
foreach($result as $row) {
$wave_buoy_ref_value = $row->nid;
//print_r($row);
}

Then when building the node, added the following: 然后,在构建节点时,添加以下内容:

// Reference Wave Buoy
$node->field_buoy_ref[$node->language][0]['target_id'] = $wave_buoy_ref_value;

Lee 背风处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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