<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Sigh. &nbsp;It's amazing how writing a message to a HUGE support list prompts you to look at things missed and fix your own problem.<div><br></div><div>In my case, I went back and looked at the documentation for hook_migrate_complete_{destination} and realized that the node was already saved.</div><div><br></div><div>&nbsp; "The complete hook is called after successfully saving the migrated object -
  this allows other modules to perform operations that require the object to
  already exist (and have an ID), such as save data related to that object."</div><div><br></div><div>In other words, the node isn't going to be saved again after this hook. &nbsp;However, &nbsp;the node is saved after the prepare hook. &nbsp;I had initially tried hook_migrate_prepare_node(), but it hadn't worked there, probably because of bugs I fixed after moving it to the complete hook. &nbsp;I moved my code back to the prepare hook, and it works like a charm.</div><div><br></div><div>Remember boys and girls, always read the documentation carefully...</div><div><br></div><div>Steve</div><div><div><br><div>Begin forwarded message:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1);"><b>From: </b></span><span style="font-family:'Helvetica'; font-size:medium;">Steve Edwards &lt;<a href="mailto:killshot91@gmail.com">killshot91@gmail.com</a>&gt;<br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1);"><b>Date: </b></span><span style="font-family:'Helvetica'; font-size:medium;">September 21, 2011 1:00:32 PM PDT<br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1);"><b>To: </b></span><span style="font-family:'Helvetica'; font-size:medium;"><a href="mailto:support@drupal.org">support@drupal.org</a><br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1);"><b>Subject: </b></span><span style="font-family:'Helvetica'; font-size:medium;"><b>Getting fid for saved file</b><br></span></div><br><div>I'm using Migrate module to import and update nodes with data from external sources, and part of the process involves getting an image from a remote URL and saving it to an imagefield(uniquely named field_image) in the imported node. &nbsp;In my case, I'm using the hook_migrate_complete_node() hook to get the image and update the $node object. &nbsp;I've borrowed from Matt Butcher's post (<a href="http://technosophos.com/content/simple-pattern-importing-images">http://technosophos.com/content/simple-pattern-importing-images</a>) to get the following code:<br><br> &nbsp;$image_path = $url = $row-&gt;ridethis_combined_image;<br> &nbsp;$images_dir = file_directory_path().'/products/';<br> &nbsp;$image_path = substr($image_path, strpos($image_path, "/")+1);<br> &nbsp;$image_name = substr($image_path, strrpos($image_path, "/")+1); &nbsp;<br> &nbsp;$returned_image = drupal_http_request($url);<br><br> &nbsp;if (file_save_data($returned_image-&gt;data, $images_dir.$image_name, FILE_EXISTS_REPLACE)) {<br> &nbsp;&nbsp;&nbsp;$asset = new stdClass();<br> &nbsp;&nbsp;&nbsp;$asset-&gt;filename = $image_name;<br> &nbsp;&nbsp;&nbsp;$asset-&gt;uid = $node-&gt;uid;<br> &nbsp;&nbsp;&nbsp;$asset-&gt;nid = $node-&gt;nid;<br> &nbsp;&nbsp;&nbsp;$asset-&gt;filepath = $images_dir.$image_name;<br> &nbsp;&nbsp;&nbsp;$asset-&gt;filemime = file_get_mimetype($asset-&gt;filename);<br> &nbsp;&nbsp;&nbsp;$asset-&gt;timestamp = time();<br> &nbsp;&nbsp;&nbsp;$asset-&gt;status = 1;<br> &nbsp;&nbsp;&nbsp;drupal_write_record('files', $asset);<br><br> &nbsp;&nbsp;&nbsp;$node-&gt;field_image[] = (array)$asset;<br> &nbsp;//node_save($node);<br> &nbsp;}<br><br>The image is downloaded to the appropriate directory (sites/default/files/products) and the record is correctly saved in the files table. &nbsp;I put a breakpoint in my code and checked $node-&gt;field_image, and the $asset data is there. &nbsp;However, the image is not being displayed in the node. &nbsp;Upon inspection of the records in content_type_product, I (finally) noticed that the field_image_fid field was empty for each record. &nbsp;When I manually entered the appropriate fid from the files table, the image shows up fine when viewing/editing the node, and when displayed in a view.<br><br>So, after all that explanation, the question is, how do I get the fid once the record has been saved to the files table? Per the docs for drupal_write_record():<br><br> &nbsp;The $object parameter contains values for any serial fields defined by the $table. For example, $object-&gt;nid will be populated after inserting a new node.<br><br>which means that after the call to drupal_write_record(), the $asset object contains the fid (as $asset-&gt;fid) when it is put into $node-&gt;field_image[] (I verified this by examining the $asset object with a breakpoint in a debugger prior to adding it to $node, and also verified that $node-&gt;field_image[]['fid'] was populated after that).<br><br>The interesting thing about this is that if I use the exact same code in a custom action, it works fine. &nbsp;The only difference is that I do a node_save() right after adding the data to $object-&gt;field_image, where in the migrate hook I'm relying on migrate module to save the node. &nbsp;Based on that, I'm leaning towards the problem being with Migrate, but I wanted to check to see if there's something obvious I might be missing.<br><br>Thanks.<br><br>Steve</div></blockquote></div><br></div></body></html>