I always start with how things are done in the CMS. If you study the
save() method of the file
administrator/components/com_categories/models/category.php, you'll
see the steps done saving a category using the API. Now, a lot of
those steps might not be needed, depending on how much control you
have over the data coming in. The key method is JTableCategory store()
($table->store()). That does the SQL to insert the new row into the
table.
Then we do the rebuildPath() and rebuild() methods. I'm not sure why
we need the rebuildPath() there to be honest. If you are doing a big
import, you could probably just do one rebuild of the entire table
when you are done. You would probably need to test this.
You could also look at Matias' JUpgrade extension
(
http://extensions.joomla.org/extensions/migration-a-conversion/joomla-migration/11658)
and see how he handles converting categories from 1.5 to 2.5. He must
have solved the same issue.
The most basic steps are to populate the table's properties (one per
column) and then call the table's store() method. Here is an example
of some code Hannes Papenburg wrote years ago to create a bunch of
dummy categories for testing:
<?php
$itemsPerLevel = 5;
$level = 4;
recursiveCategoryGenerator($level, $itemsPerLevel);
echo 'done';
function recursiveCategoryGenerator($level, $itemsPerLevel, $parent = 29)
{
if($level > 0)
{
for($i = 0; $i < $itemsPerLevel; $i++)
{
$table = JTable::getInstance('category');
$table->setLocation($parent, 'last-child');
$table->extension = 'com_content';
$table->title = 'Level'.$level.'Item'.$i;
$table->alias = 'level'.$level.'item'.$i;
$table->description = '';
$table->published = 1;
$tabel->access = 1;
$table->params = '{}';
$table->check();
$table->store();
//echo $table->id;var_dump($table);die;
recursiveCategoryGenerator($level - 1, $itemsPerLevel, $table->id);
}
}
}
You can see he's just populating a few of the table's fields and then
calling the check() and store() methods. The check() method just makes
sure you have the required values and that the row is valid. Note that
he is NOT setting lft and rgt. These are set for you by the store()
method.
Hope that helps a bit. Good luck. Mark