我试图将我的数据库输出(目前每个孩子和孙子从某些类别分配给我)的输出限制为仅该孩子。这样就不会显示孙子。
我试图将递归限制为仅发生一次,但是它不起作用。我认为答案在于数据库查询,但我对数据库并不熟悉。
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
答案 0 :(得分:0)
用户mickmackusa的评论解决了该问题。他的解决方案效果很好!
Rails.application.routes.draw do
resources :allergies
resources :prescriptions
resources :issues
get 'issues/:id/resolve' => 'issues#resolve', as: :resolve_issue
get 'notes/:id/sign' => 'notes#sign', as: :sign_note
get 'notes/:id/hide' => 'notes#hide', as: :hide_note
get 'patients/:id/show' => 'patients#show', as: :show_patient
resources :notes
resources :observations
resources :patients
end