有谁知道如何以编程方式正确地向数据库插入新的内容类别? 对于类别表中的每个帖子,还有一个帖子保存在资产表中,并且设置了lft和rgt。 是否有任何原生Joomla类可以用来代替普通SQL?如何在Joomla 2.5中添加类别
2
A
回答
0
您也许可以在category.php文件中使用save()。
文件位置:root\administrator\components\com_categories\models\category.php
它节省了提供给它的表单数据!
JOS_assets表用于存储创建的每个资产的ACL。
如果在编程式创建类别时未更新此表,则将应用默认ACL。之后当您在管理面板中打开并保存该类别时,ACL将会更新,正如核心Joomla!应该已经更新一样。
虽然您可以非常轻松地创建SQL查询并更新资产表。一旦你在phpmyadmin中打开表格的内容就很容易理解。
2
请仅使用本地类,这些类将为您无缝地处理。只要你添加类别,整个事情将被自动处理。看看任何核心组件,看看如何。
使用sql更新资产表并不容易,它都是非常特别的管理和复杂的外键系列表的一部分。
扩展JTable或JTableContent来处理这个问题。
1
这是我为此创建的一个函数,在一些挖掘&实验之后。
它使用核心类,所以它需要访问它们(对我来说它基本上是Joomla组件的一部分)。
心灵,它是为Joomla 3,为Joomla 2.5之前,您需要更改JModelLegacy到JModel。
function createCategory($name, $parent_id, $note)
{
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_categories/tables');
$cat_model = JModelLegacy::getInstance('Category', 'CategoriesModel');
$data = array (
'id' => 0,
'parent_id' => $parent_id,
'extension' => 'com_content',
'title' => $name,
'alias' => '',
'note' => $note,
'description' => '',
'published' => '1',
'access' => '1',
'metadesc' => '',
'metakey' => '',
'created_user_id' => '0',
'language' => '*',
'rules' => array(
'core.create' => array(),
'core.delete' => array(),
'core.edit' => array(),
'core.edit.state' => array(),
'core.edit.own' => array(),
),
'params' => array(
'category_layout' => '',
'image' => '',
),
'metadata' => array(
'author' => '',
'robots' => '',
),
);
if(!$cat_model->save($data))
{
return NULL;
}
$categories = JCategories::getInstance('Content');
$subcategory = $categories->get($cat_model->getState("category.id"));
return $subcategory;
}
2
下面是一些代码,我只是鞭打在一起,只是使用JTableCategory
类,所以可以简单地用正面或管理方的Joomla
$table = JTable::getInstance('category');
$data = array();
// name the category
$data['title'] = $title;
// set the parent category for the new category
$data['parent_id'] = $parent_id;
// set what extension the category is for
$data['extension'] = $extension;
// Set the category to be published by default
$data['published'] = 1;
// setLocation uses the parent_id and updates the nesting columns correctly
$table->setLocation($data['parent_id'], 'last-child');
// push our data into the table object
$table->bind($data);
// some data checks including setting the alias based on the name
if ($table->check()) {
// and store it!
$table->store();
// Success
} else {
// Error
}
你自然会想要得到的数据片正确设置,但这些是设置的核心部分。
相关问题
- 1. 如何在joomla中的项目中添加mutilple类别2.5
- 2. 的Joomla 2.5添加父别名体类
- 3. Joomla 2.5 joomshopping类别模板
- 4. joomla 2.5类别崩溃
- 5. 在Joomla 2.5中,如何在“列出所有类别”布局中添加类别图像和标题
- 6. Joomla 2.5 +如何在url中显示类别
- 7. 在Joomla中自动添加新类别
- 8. 如何添加“无效”类形成元素的Joomla 2.5
- 9. 如何在joomla 2.5注册表单中添加额外字段?
- 10. 如何在joomla 3和2.5中添加额外的字段?
- 11. 如何在joomla 2.5中向用户programmaticaly添加注释?
- 12. 如何在Joomla 2.5中添加样式到JCE编辑器?
- 13. 如何在Joomla中添加tinymce! 2.5组件
- 14. 如何在Joomla 2.5中添加滑动横幅
- 15. 如何在Joomla 2.5中添加新的模块位置?
- 16. 添加JS中的Joomla 2.5插件
- 17. Joomla 2.5:如何从URL中删除父类别?
- 18. 迁移类别/部分到joomla 2.5
- 19. Joomla 2.5!无法使用类别字段
- 20. 如何在joomla 2.5中创建joomla用户组2.5
- 21. 如何的Joomla 2.5
- 22. 如何的Joomla 2.5
- 23. Joomla 2.5首页别名
- 24. 在K2组件上添加自定义字段类型joomla 2.5
- 25. 如何为joomla 2.5中的联系人添加多个图像?
- 26. 添加资料图片提交joomla 2.5
- 27. Joomla 2.5 - 为iPad添加自定义CSS
- 28. Joomla 2.5添加选项组到文章
- 29. Joomla 2.5显示文章/类别不完整的添加/编辑表单
- 30. Joomla 2.5在博客视图中显示子类别图像
请不要这样做。应始终不要直接触摸资产表,只能通过使用具有资产字段的表进行修改。管理嵌套集合并非一帆风顺。 – Elin 2013-02-21 22:43:02