Im working on migrating content from our old db into Drupal. I was able to import content from the old db and create nodes using the script below. The CCK Content Type contains an asset field for images which i was also able to import. But the problem is that when im on the edit page of that node the asset module doesnt pre populate the asset image fields. In addition im able to view the values of those fields when im vieweing using "Dev Load".

Is there a way that I can have asset module display the images on nodes that have been generated automatically by a script ??

Thanks


require_once './includes/bootstrap.inc'; 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

        global $user;
		$result =   db_query(db_rewrite_sql("select * from dbname.dbtable"));
		while($results = db_fetch_object($result))
		{
		
			$node = new StdClass();
			$node->type = 'cck_content_type';  
			$node->uid = $user->uid;
			$node->status = 1;  
			$node->promote = 0;  
			$node->sticky = 0;  
			$node->title = $results->TITLE;
			$node->body = $results->TEXT;
			$node->field_story_id[0]['value'] = $results->STORYID; 
			$node->field_datel[0]['value'] = $results->SUBMITDATE;
			$node->field_datel[0]['value'] .= 'T00:00:00';
			$node->field_breakpoint[0]['value'] = $results->breakpoint; 	

			$images =  db_query("select imagename from dbname.pictures where STORYID = $results->STORYID");
			$imageCount = db_num_rows($images);

			$i=0;
			while($image = db_fetch_object($images))
			{
				$assetId = db_next_id('{asset}_aid');
				$imagename = $image->imagename. '-460.jpg';
				
				$node->field_story_images[$i]['options'] = $imagename;
				$node->field_story_images[$i]['caption'] = $imagename;
				$asset_type = 'local';
				$asset_dirname = 'images';
				$asset_extension = 'jpg';
				$asset_uid = 1;			
	
				db_query("insert into {asset} (aid, type, dirname, extension, filename, uid, status) values (%d, '%s', '%s', '%s', '%s', %d, %d)", $assetId, $asset_type, $asset_dirname, $asset_extension, $imagename, 1, 1);
				$i++;
			}
	
			node_save($node);
			$nid = $node->nid;
		}

Comments

janwari’s picture

got it working :)

Just needed to add a single line of code
$node->field_story_images[$i]]['aid'] = $assetId;

wmostrey’s picture

Status: Active » Fixed

Glad to see you got it working, good job :) You could also have used asset_save() of course.

janwari’s picture

If i had to do it using asset_save() how can I do it ?? I searched but wasnt able to find any documentation on asset_save()

wmostrey’s picture

That's probably because there isn't any I'm afraid, the asset api or the code in general isn't documented yet. The only way to see how asset_save works, is to look at the code (asset.module).

petermo’s picture

I have a related migration request... I have imported several hundred assets using Asset Import. I have a content type (called FcaDocument), which has an asset field, and thus want to populate several hundred FcaDocuments each with their appropriate asset. My PHP is not very strong, but I can drive PHPMyAdmin reasonably well, so have a working knowledge of nids and vids and aids. I can easily make a list specifying "FcaDocument with node ID xxxx needs to have asset ID yyyy added to it". Any help much appreciated.

janwari’s picture

you could write a script for each FcaDocuments which gets the related asset and adds it to a CCK field.

petermo’s picture

Thanks, can you give me some pointers, on what such a script might look like.... anything similar would be a help.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

Anonymous’s picture

Issue summary: View changes