控制器在CI中未加载模型

时间:2012-08-04 07:04:33

标签: php codeigniter

所以这是我的代码 -

控制器

public function addCategory() {
    if($this->session->userdata('logged_in') == TRUE) {
        $this->load->model("categories");
        echo $this->categories->addCategory();
    }
}

型号

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Categories extends CI_Model {

    // Retrieves all category names and id's
    function getCategories() {

    }

    // Add category to database. If there are errors returns message to AJAX controller
    function addCategory() {
        return "yeah";
        das
        $categoryName = $_POST['categoryName'];
        $parentCategory = $_POST["parentCategory"];
        $error = "";
        if(isset($categoryName) && isset($parentCategory) && $categoryName != "") {
            $query = $this->db->get_where("categories", array("name" => $categoryName));
            if($query->num_rows() > 0) {
                $error = "Category with that name already exists!";
            }
            else {
                $data = array(
                    'name' => $categoryName,
                    'parent' => $parentCategory
                );
                $this->db->insert('categories', $data);
            }
        }
        else {
            $error = "Category Name can't be empty.";
        }
        if($error != "") {
            return $error;
        }
        else {
            return "Success! Category has been added!";
        }
    }

}

正如你所看到的,我特意添加了返回“是”和das,看看它是否有效,但它没有,它只是返回空白页而没有错误。此外,这是AJAX请求,如果我在控制器内添加模型内容,它的工作完美。

可能是什么问题?如果您需要任何其他信息,请给我一个通知。

编辑:再一次,Das只是我尝试的东西,并没有显示任何错误,这里是我当前的模型代码没有未使用的条目 -

// Add category to database. If there are errors returns message to AJAX controller
function addCategory() {
    $categoryName = $_POST['categoryName'];
    $parentCategory = $_POST["parentCategory"];
    $error = "";
    if(isset($categoryName) && isset($parentCategory) && $categoryName != "") {
        $query = $this->db->get_where("categories", array("name" => $categoryName));
        if($query->num_rows() > 0) {
            $error = "Category with that name already exists!";
        }
        else {
            $data = array(
                'name' => $categoryName,
                'parent' => $parentCategory
            );
            $this->db->insert('categories', $data);
        }
    }
    else {
        $error = "Category Name can't be empty.";
    }
    if($error != "") {
        return $error;
    }
    else {
        return "Success! Category has been added!";
    }
}

2 个答案:

答案 0 :(得分:2)

重命名class name Categories to Categories_model并命名file as categories_model.php。您可能无法访问模型文件,因为您在模型文件中放入了错误的类名

答案 1 :(得分:1)

PHP无法识别

das,因此它无声地说unexpected T_VARIABLE

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Categories extends CI_Model {

    // Retrieves all category names and id's
    function getCategories() {

    }

    // Add category to database. If there are errors returns message to AJAX controller
    function addCategory() {
        return "yeah";
        $categoryName = $_POST['categoryName'];
        $parentCategory = $_POST["parentCategory"];
        $error = "";
        if(isset($categoryName) && isset($parentCategory) && $categoryName != "") {
            $query = $this->db->get_where("categories", array("name" => $categoryName));
            if($query->num_rows() > 0) {
                $error = "Category with that name already exists!";
            }
            else {
                $data = array(
                    'name' => $categoryName,
                    'parent' => $parentCategory
                );
                $this->db->insert('categories', $data);
            }
        }
        else {
            $error = "Category Name can't be empty.";
        }
        if($error != "") {
            return $error;
        }
        else {
            return "Success! Category has been added!";
        }
    }

}