致命错误:未捕获错误:在

时间:2016-01-15 11:32:41

标签: php database oop model-view-controller

我对php中的OOP有点新,我每次都在模型中遇到这个问题,但是我找不到导致这个问题的错误。

我收到错误消息:致命错误:未捕获错误:在getCategories函数的模型中调用null成员函数select()(参见下面的代码)。

<?php

class model_additem extends model{
    public function __construct(){
        DB::setConnection(DB_ADMIN,DB_ADMIN_PASS);
    }

    public function insertItem($picName,$n,$d,$p,$ui,$ic,$pt,$pd="good"){
        $data = ["pic_name"=>$picName,"pic_desc"=>$pd,"pic_type"=>$pt];
        $img = parent::$db->saveTo("pictures")->setData($data)->execute();

        if($img){
            $imgId = parent::$db->lastInsertId();

            $data = ["name"=>$n,"description"=>$d,"price"=>$p,"image_id"=>$imgId,"owner_id"=>$ui,"category_id"=>$ic];
            $prod = parent::$db->saveTo("products")->setData($data)->execute();
            return $prod;
        }
        return false;
    }

    public function getCategories(){
        $data = parent::$db->select()->from('category')->fetch('all');
        if($data) return $data;
        return false;
    }


}

这是我的 CONTROLLER 文件:

<?php

class controller_additem extends controller{
    public function __construct(){
        Load::view("admin".DS."header");
        Load::view('additem/additem');
        Load::view("admin".DS."footer");
        require_once 'CheckAdmin.php'; // Check if the user is admin to enter page + more.


    }

    public function index(){
        $item = $this->model("additem");
        if(isset($_POST["addItem"])){
            $iName = filter_input(INPUT_POST,"iName",FILTER_SANITIZE_MAGIC_QUOTES);
            $iDesc = filter_input(INPUT_POST,"iDesc",FILTER_SANITIZE_MAGIC_QUOTES);
            $iPrice = filter_input(INPUT_POST,"iPrice",FILTER_SANITIZE_NUMBER_FLOAT);
            $iCat = filter_input(INPUT_POST,"category",FILTER_SANITIZE_NUMBER_INT);
            $iPic = $_FILES["iPicture"];

            $extention = explode(".",$iPic["name"]);
            $ext = end($extention);
            //pre($iPic);
            //validation
            //if($iPic["size"]> 2000000){}
            $picName = $iName.time().'.'.$ext;
            if(move_uploaded_file($iPic["tmp_name"],ROOT . "public/images/" . $picName)){
                $userId = Session::get("userId");

                $result = $item->insertItem($picName,$iName,$iDesc,$iPrice,$userId,$iCat,$ext);
                if($result){
                    Session::set("result","Item has been inserted successfully");
                }else{
                    Session::set("result","Error occured");
                }

            }
        }
        $cats = $item->getCategories();
        $uName = Session::get("userData")["name"];
        Load::view("additem/additem",$cats);
    }
}

我的引擎文件中还有一个db类,它处理与数据库相关的函数,而select函数就在那里:

class db{
    private static $db = null;
    private $sql = "";
    private $save_type;
    private $binded = array();
    private $query = '';
    private $last_insert_id;

    public function __construct(){
        if(self::$db == null){
            $user = DB_GUEST;
            $pass = DB_GUEST_PASS;
            if(Session::found("user_priv") == 'no'){
                $user = DB_USER;
                $pass = DB_USER_PASS;
            }
            if(Session::found("user_priv") == 'yes'){
                $user = DB_ADMIN;
                $pass = DB_ADMIN_PASS;
            }
            self::setConnection($user,$pass);
        }
    }

    public static function setConnection($user,$pass,$dbname=DB_NAME){
        try{
            self::$db = new PDO("mysql:host=localhost;dbname=$dbname",$user,$pass);
            self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
            self::$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            self::$db->exec("SET NAMES utf8");
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }

    public static function getDb(){
        if(self::$db == null){
            $user = DB_GUEST;
            $pass = DB_GUEST_PASS;
            if(Session::found("userId")){
                $user = DB_USER;
                $pass = DB_USER_PASS;
            }
            if(Session::found("admin")){
                $user = DB_ADMIN;
                $pass = DB_ADMIN_PASS;
            }
            self::setConnection($user,$pass);
        }
        return self::$db;
    }

    //SELECT [name,pass,email]
    public function select(array $select=[]){
        $sql = "SELECT ";
        if(empty($select)){
            $sql .= "*";
        }else{
            filter_array($select);
            $select = implode(",",$select);
            $sql .= string($select);
        }
        $this->sql = $sql;

        return $this;
    }

    //FROM
    public function from($tbl){
        $this->sql .= " FROM " . string($tbl);
        return $this;
    }

    //WHERE
    public function where(array $where){
        //filter_array($where);
        $sql = " WHERE ";
        foreach($where as $key => $val){
            $sql .= $key . " LIKE '".$val."' AND ";
        }

        $this->sql .= rtrim($sql," AND ");
        return $this;
    }

    //JOIN
    public function join(array $tbl,$type="LEFT JOIN"){
        $sql = " ".strtoupper($type)." ";
        $sql .= implode(",",$tbl);

        $this->sql .= $sql;
        return $this;
    }

    //ON
    public function on($cond1,$cond2){
        $sql = " ON ";
        $sql .= string($cond1) . " = " . string($cond2);

        $this->sql .= $sql;
        return $this;
    }

    //ORDER BY
    public function order_by($order_by, $order_type="ASC"){
        $sql = " ORDER BY ";
        $sql .= string($order_by)." ".strtoupper(string($order_type));

        $this->sql .= $sql;
        return $this;
    }

    //LIMIT
    public function limit($limit){
        $this->sql .= " LIMIT " . int($limit);
        return $this;
    }

    //FETCH
    public function fetch($fetch_type=""){
        $fetch = "fetch";
        if($fetch_type){
            $fetch = "fetchAll";
        }

        try{
            $query = self::$db->query($this->sql);
            $this->reset();
            return $query->$fetch();
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }

    //saveTo
    public function saveTo($tbl,$type="insert"){
        $tbl = string($tbl);
        $this->save_type = strtolower(string($type));

        if($this->save_type == "update"){
            $sql = "UPDATE ";
        }elseif($this->save_type == "replace"){
            $sql = "REPLACE INTO ";
        }else{
            $sql = "INSERT INTO ";
        }

        $sql .= $tbl . " SET ";
        $this->sql = $sql;
        return $this;
    }

    //SET DATA
    public function setData(array $data,$filter="string"){
        $filter = string($filter);

        foreach($data as $colomn => $value){
            $colomn = string($colomn);
            $value = $filter($value);
            $this->binded[$colomn] = $value;
            $this->sql .= $colomn . "=:" . $colomn . ",";
        }
        //pre($this->binded);
        $this->sql = rtrim($this->sql,",");
        return $this;
    }

    //execute
    public function execute(){
        try{
            $this->query = self::$db->prepare($this->sql);
            if($this->binded){
                foreach($this->binded as $colomn => $value){
                    $this->bind($colomn,$value);
                }
            }
            $result = $this->query->execute();

            if($this->save_type == 'insert'){
                $this->last_insert_id = self::$db->lastInsertId();
            }
            $this->reset();
            return $result;

        }catch(PDOException $e){
            die($e->getMessage());
        }
    }

    //BIND
    private function bind($placeholder,$value,$filter="string",$bind_type="bindValue"){
        return $this->query->$bind_type(":".$placeholder,$filter($value),PDO::PARAM_STR);
    }

    public function lastInsertId(){
        return $this->last_insert_id;
    }

    //DELETE
    public function delete($tbl,$col=false){
        $this->sql = "DELETE ";
        if($col){
            $this->sql .= string($col)." ";
        }
        $this->sql .= "FROM " . string($tbl);
        return $this;
    }

    //TRUNCATE
    public function truncate($tbl){
        $this->sql = 'TRUNCATE ' . string($tbl);
        return $this;
    }

    //View Query
    public function viewQ(){
        echo $this->sql;
    }

    //RESET
    private function reset(){
        $this->sql = "";
        $this->save_type = "";
        $this->query = "";
        $this->binded = array();
    }
}

问题是,如果你在saveTo函数中查看模型,它似乎工作。但是,select不会,也不会是它之后的函数。

请帮助我,我试图搜索此错误但找不到我的特定问题的解决方案。

非常感谢任何帮助! 提前谢谢。

编辑:感谢大家的帮助!我想到了。模型类中存在错误,因此没有与db连接。

3 个答案:

答案 0 :(得分:6)

似乎$ db未在父类中定义。哪个是型号。你确定你初始化了吗?

如果在父构造函数(在类模型中)初始化它,则需要在当前构造函数中显式调用它(在类model_addItem中)。 PHP不会自动执行此操作。

答案 1 :(得分:2)

该错误表明在select()对象上调用了null。在var_dump()上执行parent::$db并确保该对象实际上不为空。

答案 2 :(得分:1)

您的问题是parent::$dbnull。这意味着数据库连接未初始化。从弄清楚原因和从那里开始工作。