dongyingjiu0669 2012-11-05 23:58
浏览 100

动态Drupal节点创建期间的数据库查询以获取实体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). I can add a static entity id no problem:

// 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? For your reference this is the current script i am using (thanks to 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!
";
}

thanks

Lee

  • 写回答

1条回答 默认 最新

  • duan1930 2012-11-06 21:43
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么