我正在尝试将我的数据库输出(目前它从某些类别中为我提供每个孩子和孙子的某些类别)仅限于孩子。这样就不会显示孙子。
我试图将递归限制为只发生一次,但它不起作用。我认为答案在于db查询,但我对dbs并不熟悉。
public function getChildCategories(&$result, &$categories, $level = 1)
{
$db = EB::db();
$ordering = $this->params->get('order', 'popular');
$sort = 'desc';
$total = (int) $this->params->get('count', 0);
$hideEmptyPost = $this->params->get('hideemptypost', false);
$language = EB::getCurrentLanguage();
foreach ($result as $row) {
// Initialize default structure
$category = EB::table('Category');
$category->bind($row);
$category->cnt = $row->cnt;
$categories[$row->id] = $category;
$categories[$row->id]->childs = array();
// Find child categories
$query = array();
$query[] = 'SELECT a.*, COUNT(' . $db->qn('b.id') . ') AS ' . $db->qn('cnt') . ',' . $db->Quote($level) . ' AS ' . $db->qn('level');
$query[] = 'FROM ' . $db->qn('#__easyblog_category') . ' AS a';
$query[] = 'LEFT JOIN ' . $db->qn('#__easyblog_post_category') . ' AS pc';
$query[] = 'ON ' . $db->qn('a.id') . '=' . $db->qn('pc.category_id');
$query[] = 'LEFT JOIN ' . $db->qn('#__easyblog_post') . ' AS b';
$query[] = 'ON ' . $db->qn('b.id') . '=' . $db->qn('pc.post_id');
$query[] = 'AND ' . $db->qn('b.published') . '=' . $db->Quote(EASYBLOG_POST_PUBLISHED);
$query[] = 'AND ' . $db->qn('b.state') . '=' . $db->Quote(EASYBLOG_POST_NORMAL);
$query[] = 'WHERE ' . $db->qn('a.published') . '=' . $db->Quote(1);
$query[] = 'AND ' . $db->qn('parent_id') . '=' . $db->Quote($row->id);
if ($language) {
$query[] = 'AND(';
$query[] = $db->qn('a.language') . '=' . $db->Quote($language);
$query[] = 'OR';
$query[] = $db->qn('a.language') . '=' . $db->Quote('');
$query[] = 'OR';
$query[] = $db->qn('a.language') . '=' . $db->Quote('*');
$query[] = ')';
}
if (!$hideEmptyPost) {
$query[] = 'GROUP BY ' . $db->qn('a.id');
} else {
$query[] = 'GROUP BY ' . $db->qn('a.id') . ' HAVING (COUNT(' . $db->qn('b.id') . ') > 0)';
}
if ($ordering == 'ordering') {
$query[] = ' ORDER BY `lft` desc';
}
if ($ordering == 'popular') {
$query[] = ' ORDER BY `cnt` desc';
}
if ($ordering == 'alphabet') {
$query[] = ' ORDER BY a.`title` asc';
}
if ($ordering == 'latest') {
$query[] = ' ORDER BY a.`created` desc';
}
$query = implode(' ', $query);
$db->setQuery($query);
$children = $db->loadObjectList();
$ccounter = 0;
// Recursion happens here
if ($children) {
$this->getChildCategories($children, $categories[$row->id]->childs, ++$level);
}
}
}
有人可以向我解释如何修改数据库查询吗?现在的输出是:
Category
-Child Cat
--Grandchild Cat
-Child Cat
我想:
Category
-Child Cat
-Child Cat