I realise I'm probably recovering things that have already been asked but I'm still a drupal n00b and spent quite a while trying to work this out already.

I have two tables in SQL (categories and companies), each company has a category identified by an index into the categories table. I have it in table wizard, can make the relationship and that's all great.

Now I want to import these into two different content types, with the company nodes having a CCK node reference to a category node. After attempting with the ui for a bit I figured I probably need to import the categories first and then import the companies with a migrate_prepare_hook that would figure out and set the nid to the appropriate category node.

That's where I'm stuck at the moment - nothing I so seems to end up in the database. Currently I have

function mymodule_migrate_prepare_node(&$node, $tblinfo, $row) {
  $node->field_org_cat_nid[0]['nid'] = 567;
}

(with 567 being a nid I looked up) but it's having no effect. When I dump $node out I don't see any entry for my field_org_cat field; is that part of the problem?

Any help appreciated.

Comments

timidri’s picture

Hi skitten,

I have successfully imported multiple interrelated tables into interrelated nodes of different types using table wizard and migrate. The procedure is not obvious but reasonably straightforward:

1. Start by defining the view and the content set for the table without reference (in your case, category). Give this content set a weight of, say, 0. When you save the content set, you will find two extra tables created by migrate: the migrate_map_* and migrate_msg_*. These names will be either postfixed with your table names or with numbers, depending on the version of the migrate module you are using. After importing categories the map table for categories will contain, for each record in your imported table, the primary key of the table in the sourceid column and the corresponding nid of the imported node in the destid column.

2. Now, analyze the company table. Make sure you check the "Available key" check box for the foreign key column.

3. Define a relationship (admin/content/tw/relationships) from your company table's foreign key to the category's map table's sourceid. Personally, I prefer setting the "Incorporate related table into views automatically:" pull-down to "Manual" and add relationships to my view later myself.

4. Now, edit the generated view for the company table. Add the relationship you just created to the view. Also add a field "migrate_map_*.destid" to the view. This will be the nid for the category node which this company should get related to. Give it a recognizable label, for instance "category nid".

5. Finally, add a content set for the company table. Map the field "category nid" from your company view to the node reference field in your company node. Save the content set with a weight of 1.

6. Now, you can import both tables. The order of content sets is important; we need first to import the categories in order to populate the map table. Proper setting of weights of your content sets will ensure this.

That's it! If you have multiple interrelated tables, you just repeat steps 2 through 5 for each table, while paying attention to relate to the correct map tables.

Hope this helps!

skitten’s picture

Thanks! That was exactly what I was looking for.

I figured it was probably possible, just couldn't work out how. Sign me up for contributing documentation for this...

skitten’s picture

Next question ;)

Now I need a simple many-to-many relationship (students to classes type deal) - one side is just a a set of strings so i was thinking of making a taxonomy and doing it that way, but i'm not sure how i'd migrate the relationship data in. Is there a set way to do many-to-many?

mikeryan’s picture

many-to-many is really many one-to-manys... So, if classes are your set of strings, implement them as taxonomy terms. Set up a content set for students (one student per row in the view). This view would not include the classes, which would generate multiple rows per student. Thus, you need to deal with them in your prepare_node() hook (assuming you're representing the students as nodes). Query the classes related to the current student, and for each name do taxonomy_get_term_by_name() and add them to $node->taxonomy[].

Does this help?

skitten’s picture

Yes - that's working for me. Now I'm thinking my categories should have been taxonomy terms too, so I'm going to go back and reimport all my data...

moshe weitzman’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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

mrbeau’s picture

dbt,

Thanks very much for your description. I spent a lot of time trying to get this to work and after following your directions I was able to finally import my relational data! It seems a bit strange that you have to manually specify the views etc... Is this a bug, as it seems that it should all be automated between Table Wizard and Migrate. Without following your exact steps, I could not get it to work at all.

Cheers,
Matt

shambler’s picture

I'm in the same boat as #5 - how do I still import related tables but with some of them as taxonomies - is that possible?