<?php
require_once JPATH_ADMINISTRATOR . '/components/com_categories/models/category.php';
require_once JPATH_ADMINISTRATOR . '/components/com_categories/tables/category.php';//引入2个目录组件的文件,以便使用<span style="font-family: Arial, Helvetica, sans-serif;">CategoriesModelCategory </span>
$new_category = new CategoriesModelCategory ();
$options = array (
'driver' => 'mysqli',
'host' => 'localhost',
'user' => 'root',
'password' => 'pwd',
'database' => 'dedecms',
'prefix' => 'dede_'
);
$db = JDatabaseDriver::getInstance ( $options ); //通过Joomla!的数据库类来读取dedecms的类表
$query = $db->getQuery(true);
$query->select($db->quoteName(array('id','typename','typedir')))
->from($db->quoteName('#__arctype'))
->order('sortrank ASC')
->where($db->quoteName('reid') . ' = '. $db->quote('0'))
->setLimit('8','47'); //最好不要一次调用太多,以防超时
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $sanjikeshi){
$keshiname3 = substr($sanjikeshi->typedir,strrpos($sanjikeshi->typedir,'/')+1,strlen($sanjikeshi->typedir)-strrpos($sanjikeshi->typedir,'/'));
$data3 = array (
'id' => 0,
'hits' => '0',
'parent_id' => '79',
'extension' => 'com_content',
'title' => $sanjikeshi->typename,
'alias' => $keshiname3,
'note' => $sanjikeshi->id, //把原来的ID存入备注中,方便之后存入文章
'published' => '1',
'access' => '1',
'language' => '*'
);
if($new_category->save($data3)){
var_dump($sanjikeshi->typename.'save to db ');
}else {
var_dump($sanjikeshi->typename.'fail ');
}
//以下是把这层目录的子目录读出来,然后存到这层子目录下
$par_id = $new_category->getState('category.id');
$query2 = $db->getQuery(true);
$query2->select($db->quoteName(array('id','typename','typedir')))
->from($db->quoteName('#__arctype'))
->order('sortrank ASC')
->where($db->quoteName('reid') . ' = '. $sanjikeshi->id);
$db->setQuery($query2);
$results4 = $db->loadObjectList();
foreach($results4 as $sijikeshi){
$keshiname4 = substr($sijikeshi->typedir,strrpos($sijikeshi->typedir,'/')+1,strlen($sijikeshi->typedir)-strrpos($sijikeshi->typedir,'/'));
$data4 = array (
'id' => 0,
'hits' => '0',
'parent_id' => $par_id,
'extension' => 'com_content',
'title' => $sijikeshi->typename,
'alias' => $keshiname4,
'note' => $sijikeshi->id,
'published' => '1',
'access' => '1',
'language' => '*'
);
if ($new_category->save ( $data4 )) {
var_dump($sijikeshi->typename . 'save to db ');
} else {
var_dump( $sijikeshi->typename . 'fail');
}
}
}
?>
核心技术2个:
- 使用CategoriesModelCategory 类来自动写入数据库
- 使用JDatabaseDriver来都去原来dedecms中的数据
这只是个小demo,离使用还有差距