PHP模型没有返回结果

时间:2013-09-01 15:54:44

标签: php opencart

我有一个递归模型函数:

public function buildPaths ($category_id, $current_path = array()) {
    if (!empty ($current_path)):
        $output = $current_path;
    else:
        $output = array(0 => $category_id);
    endif;

    $query = $this->db->query ("
        SELECT parent_id 
        FROM {$this->prefix}category 
        WHERE category_id = '" . (int)$category_id . "' 
        AND status = '1'");

    if ($query->row['parent_id'] != 0):
        $output[] = $query->row['parent_id'];
        $this->buildPaths($query->row['parent_id'], $output);
    else:
        $output = array_reverse ($output);
        $path = implode ('_', $output);
        return (string)$path;
    endif;
}

例如,传递值为40应该返回以下内容:

3_40

当我在模型中回显变量$path时,它会显示正确的值,但是当我通过我的控制器调用该函数时,即:

$result = $this->model_catalog_category->buildPaths(40);

$result返回空。

关于为什么会发生这种情况的任何想法?

2 个答案:

答案 0 :(得分:0)

你有一个if流控制并返回on else尝试放弃其他

if ($query->row['parent_id'] != 0):
        $output[] = $query->row['parent_id'];
        $this->buildPaths($query->row['parent_id'], $output);
    else:
        $output = array_reverse ($output);
        $path = implode ('_', $output);
    endif;

        return (string)$path;

答案 1 :(得分:0)

感谢andrewsi获得此答案:

public function buildPaths ($category_id, $current_path = array()) {
    if (!empty ($current_path)):
        $output = $current_path;
    else:
        $output = array(0 => $category_id);
    endif;

    $query = $this->db->query ("
        SELECT parent_id 
        FROM {$this->prefix}category 
        WHERE category_id = '" . (int)$category_id . "' 
        AND status = '1'");

    if ($query->row['parent_id'] != 0):
        $output[] = $query->row['parent_id'];
        return $this->buildPaths($query->row['parent_id'], $output);
    else:
        $output = array_reverse ($output);
        $path = implode ('_', $output);
        return (string)$path;
    endif;
}