| Новости | FAQ | Авторы | Документация | В действии | Библиотека |
| Инструменты | Полезные ссылки | Хостинги | Скачать | Примеры | Форум |
BeaVe 11.02.2006 09:21 / 11.02.2006 13:13
Класс создан на основании статьи о построении деревье от Misha v.3.@CLASS
bcategories
@bOptions[]
$printCategory_tag_name[category]
#end @bOptions[]
@getCategories[getCategories_params]
$_getCategories_params[^hash::create[$getCategories_params]]
$result[^MAIN:pSQL.sql[table]{
SELECT
categories.category_id,
dt_published,
category_parent_id,
category_thread_id,
^if(def $_getCategories_params.SQL_SELECT){
$_getCategories_params.SQL_SELECT,
}
title
FROM
^if(def $_getCategories_params.SQL_FROM){
$_getCategories_params.SQL_FROM,
}
categories
WHERE
^if(def $_getCategories_params.SQL_WHERE){
$_getCategories_params.SQL_WHERE AND
}
categories.is_published = 1
ORDER BY
title ASC
}[
^if(^_getCategories_params.offset.int(0)){
$.offset(^_getCategories_params.offset.int(0))
}
^if(^_getCategories_params.limit.int(0)){
$.limit(^_getCategories_params.limit.int(0))
}
]]
#end @getCategories[]
@getCategoryById[category_id]
$result[
^getCategories[
$.SQL_WHERE[categories.category_id = ^category_id.int(0)]
$.limit(1)
]
]
#end @getCategoryById[]
@getCategoriesByParent[category_parent_id;limit]
$result[
^getCategories[
$.SQL_WHERE[category_parent_id = ^category_parent_id.int[]]
^if(^limit.int(0)){
$.limit(^limit.int(0))
}
]
]
#end @getCategoriesByParent[]
@getCategoriesByThread[thread]
$result[
^getCategories[
$.SQL_WHERE[
^if($thread is table){
^if($thread){
category_thread_id IN (^thread.menu{$thread.category_thread_id}[,])
}
}{
category_thread_id = ^thread.category_thread_id.int(0)
}
]
]
]
#end @getCategoriesByThread[]
@printCategory[category;body]
<${printCategory_tag_name}>
<category_id>^category.category_id.int[]</category_id>
^XML_date[
$.date[$category.dt_published]
$.tag_name[dt_published]
]
<title>$category.title</title>
$body
</${printCategory_tag_name}>
#end @printCategory[]
@printCategories[categories]
^categories.menu{
^printCategory[$categories.fields]
}
#end @printCategories[]
@printCategoriesByParent[categories;category_parent_id][level_categories]
$level_categories[^categories.select($categories.category_parent_id == $category_parent_id)]
^level_categories.menu{
^printCategory[$level_categories.fields;^printCategoriesByParent[$categories;$level_categories.category_id]]
}
#end @printCategoriesByParent[]
@category[category_params]
$category[^getCategoryById[$category_params.params.category_id]]
$result[^printCategory[$category]]
#end @category[]
@categories[categories_params]
$root_categories[^getCategoriesByParent[$categories_params.params.category_parent_id;$categories_params.params.categories_limit]]
^if($root_categories){
^if($categories_params.params.show_root_categories){
$result[^printCategories[$root_categories]]
}{
$categories[^getCategoriesByThread[$root_categories]]
^if($categories){
$result[^printCategoriesByParent[$categories;$categories_params.params.category_parent_id]]
}{
^throw[not_found;Could not find categories]
}
}
}{
^throw[not_found;Could not find root categories]
}
#end @categories[]
@auto[]
^bOptions[]
#end @auto[]Вижу есть неудобства. К примеру запросы не кэшируются... Или даже не создается хэш.^bcategories:categories[ $.params[ $.category_parent_id(2) $.show_root_categories(1) $.categories_limit(10) ] ]с вызовом ?mode=debug почему-то в sql.txt сохраняется:
SQL connections while generating page: 1 SQL requests while generating page table: 2 [0 ms/16 KB/136 blocks] -------------------------------------------------- TOTAL: 2 [0 ms/16 KB/136 blocks] TYPE: table EXECUTION TIME: 0 ms MEMORY USAGE: 8 КБ/80 blocks ROWS SELECTED: 2 SQL OPTIONS: $.limit(10) QUERY: SELECT categories.category_id, dt_published, category_parent_id, category_thread_id, title FROM categories WHERE category_parent_id = 2 AND categories.is_published = 1 ORDER BY title ASC EXPLAIN: id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE categories ref ix_categories_0,ix_categories_3 ix_categories_0 3 const,const 1 Using where; Using filesort TYPE: table EXECUTION TIME: 0 ms MEMORY USAGE: 8 КБ/56 blocks ROWS SELECTED: 4 SQL OPTIONS: $.limit(10) QUERY: SELECT categories.category_id, dt_published, category_parent_id, category_thread_id, title FROM categories WHERE category_parent_id = 1 AND categories.is_published = 1 ORDER BY title ASC EXPLAIN: id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE categories ref ix_categories_0,ix_categories_3 ix_categories_0 3 const,const 4 Using where; Using filesortНе пойму почему два запроса и WHERE category_parent_id имеет разные значения. В первом 2, а во втором 1...