Migrate d2d – Problem with duplication of thumbnails In order to be able to copy all files to certain specific subfolders in site/default/files during my migration från D6 to D7 I use the file class MigrateFileUri (as far as I understand MigrateFileFid does not care about destination directories). So, in prepareRow() I fetch the filepath from the legacy db for the main image and likewise for the thumbnail of that same product.

The problem occurs when there is only a main image and no thumbnail in a product node. What happens is that the thumbnail field gets the previuos thumbnail path used. This is my prepareRow():

public function prepareRow($row){
    // Always include this fragment at the beginning of every prepareRow()
    // implementation, so parent classes can ignore rows.
    if (parent::prepareRow($row) === FALSE) {
        return FALSE;
    }

    $fid = (isset($row-&gt;field_sensorimage) &amp;&amp; !empty($row-&gt;field_sensorimage[0])) ? $row-&gt;field_sensorimage[0]: FALSE;

    if($fid != FALSE){
        $filepath = Database::getConnection('default', 'legacy')-&gt;query('SELECT f.filepath FROM {files} f WHERE f.fid = :fid',array(':fid' =&gt; $fid))-&gt;fetchAssoc(); 
        $this-&gt;addFieldMapping('field_sensorimage_newsite')-&gt;defaultValue($filepath);           
    }

    $fid2 = (isset($row-&gt;field_thumbnail) &amp;&amp; !empty($row-&gt;field_thumbnail[0])) ? $row-&gt;field_thumbnail[0]: FALSE;

    if($fid2 != FALSE){
        $filepath = Database::getConnection('default', 'legacy')-&gt;query('SELECT f.filepath FROM {files} f WHERE f.fid = :fid',array(':fid' =&gt; $fid2))-&gt;fetchAssoc();    
        $this-&gt;addFieldMapping('field_thumbnail_newsite')-&gt;defaultValue($filepath);

        dsm($fid2);
        dsm($filepath);
    }   

    return TRUE;
    }


This happens: if let's say the 100 first nodes has no thumbnail, it works as expected. No thumbnails are copied to the destination directory. But after the first node with a thumbnail all the following nodes that did not have a thumbnail in the D6 legacy site, gets the previous nodes' thumbnail all the same in the new D7 site.

Thanks to the 2 dsm() near the end of the snippet above I can confirm that no thumbnails gets duplicated within that if statement. There are about 50 thumbnails mapped. But the end result is that several hundred nodes gets duplicate thumbnails from other nodes. 

So once the first thumbnail is migrated, that thumbnail path is inherited by all the following nodes and renamed with a number extension *_1.jog, *_2.jpg .... *_30.jpg as long as there is no thumbnail in the legacy field.

Now, where does this happen and how can I stop it from happening? I have rewritten the prepareRow() many times, but always with this same result. I am really stuck... Please help :-(
Finally found a workaorund… by deliberately giving the destination field an empty value instead of not doing anything when $fid2 is FALSE, I got rid of all the duplications :-)

if($fid2 === FALSE){
    $filepath = array();
    $this-&gt;addFieldMapping('field_thumbnail_newsite')-&gt;defaultValue($filepath);
} else {
    $filepath = Database::getConnection('default', 'legacy')-&gt;query('SELECT f.filepath FROM {files} f WHERE f.fid = :fid',array(':fid' =&gt; $fid2))-&gt;fetchAssoc();    
    $this-&gt;addFieldMapping('field_thumbnail_newsite')-&gt;defaultValue($filepath);
}