致命错误:调用未定义的方法DB :: get()

时间:2015-10-10 02:51:10

标签: php mysql mongodb

我正在访问Index.php中的数据库类。

签出文件:

db.php中

<?php namespace Blog\DB;

class DB {

    /**
    * Return db
    * @var object
    */
    private $db;
    /**
    * Results limit.
    * @var integer
    */
    public $limit = 5;


    public function __construct($config){

        $this->connect($config);    
    }

    private function connect($config){
        try{
            if ( !class_exists('Mongo')){
                echo ("The MongoDB PECL extension has not been installed or enabled");
                return false;
            }
            $connection=  new \MongoClient($config['connection_string'],array('username'=>$config['username'],'password'=>$config['password']));
            return $this->db = $connection->selectDB($config['dbname']);
        }catch(Exception $e) {
            return false;
        }
    }
    /**
     * get one article by id
     * @return array
     */
    public function getById($id,$collection){
        // Convert strings of right length to MongoID
        if (strlen($id) == 24){
            $id = new \MongoId($id);
        }
        $table = $this->db->selectCollection($collection);
        $cursor  = $table->find(array('_id' => $id));
        $article = $cursor->getNext();

        if (!$article ){
            return false ;
        }
        return $article;
    }
    /**
     * get all data in collection and paginator
     *
     * @return multi array 
     */
    public function get($page,$collection){

        $currentPage = $page;
        $articlesPerPage = $this->limit;

        //number of article to skip from beginning
        $skip = ($currentPage - 1) * $articlesPerPage; 

        $table = $this->db->selectCollection($collection);

        $cursor = $table->find();
        //total number of articles in database
        $totalArticles = $cursor->count(); 
        //total number of pages to display
        $totalPages = (int) ceil($totalArticles / $articlesPerPage); 

        $cursor->sort(array('saved_at' => -1))->skip($skip)->limit($articlesPerPage);
        //$cursor = iterator_to_array($cursor);
        $data=array($currentPage,$totalPages,$cursor);

        return $data;
    }
    /**
     * Create article
     * @return boolean
     */
    public function create($collection,$article){

        $table   = $this->db->selectCollection($collection);
        return $result = $table->insert($article);
    }
    /**
     * delete article via id
     * @return boolean
     */
    public function delete($id,$collection){
        // Convert strings of right length to MongoID
        if (strlen($id) == 24){
            $id = new \MongoId($id);
        }
        $table   = $this->db->selectCollection($collection);
        $result = $table->remove(array('_id'=>$id));
        if (!$id){
            return false;
        }
        return $result;

    }
    /**
     * Update article
     * @return boolean
     */
    public function update($id,$collection,$article){
        // Convert strings of right length to MongoID
        if (strlen($id) == 24){
            $id = new \MongoId($id);
        }
        $table   = $this->db->selectCollection($collection);
        $result  = $table->update(
                array('_id' => new \MongoId($id)), 
                array('$set' => $article)
        );
        if (!$id){
            return false;
        }
        return $result;

    }
    /**
     * create and update comment
     * @return boolean
     */
    public function commentId($id,$collection,$comment){

        $postCollection = $this->db->selectCollection($collection);
        $post = $postCollection->findOne(array('_id' => new \MongoId($id)));

        if (isset($post['comments'])) {
            $comments = $post['comments'];
        }else{
            $comments = array();
        }                   
        array_push($comments, $comment);

        return $postCollection->update(
                        array('_id' => new \MongoId($id)), 
                        array('$set' => array('comments' => $comments))
        );
    }

        public function receive($page,$collection){

        $currentPage = $page;
        $articlesPerPage = $this->limit;

        //number of article to skip from beginning
        $skip = ($currentPage - 1) * $articlesPerPage; 

        $table = $this->db->selectCollection($collection);

        $cursor = $table->find();
        //total number of articles in database
        $totalArticles = $cursor->count(); 
        //total number of pages to display
        $totalPages = (int) ceil($totalArticles / $articlesPerPage); 

        $cursor->sort(array('saved_at' => -1))->skip($skip)->limit($articlesPerPage);
        //$cursor = iterator_to_array($cursor);
        $data=array($currentPage,$totalPages,$cursor);

        return $data;
    }
}

的index.php:

<?php
require_once 'auth.php';
include '../app1.php';
include '../config.php';
include '../db.php';
require_once '../vendor/markdown/Markdown.inc.php';


use Michelf\MarkdownExtra,
    Michelf\Markdown;

$url_action = (empty($_REQUEST['action'])) ? 'logIn' : $_REQUEST['action'];

if (isset($url_action)) {
    $action = new Auth;
    if (is_callable(array($action,$url_action))) {
        call_user_func(array($action,$url_action));
    } else {
        echo 'Function does not exist, request terminated';
    }
}

if (is_array($_SESSION) &&$_SESSION['username'] ==UserAuth) {
     $data = array();
     $status = (empty($_GET['status'])) ? 'dashboard':$_GET['status'];

    switch ($status) {
        case 'create':

            if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
                $article               = array();
                $article['title']      = $_POST['title'];
                $article['html']    = Markdown::defaultTransform($_POST['content']);
                $article['content']    =$_POST['content'];

                $article['saved_at'] = new MongoDate();

                if ( empty($article['title']) || empty($article['content']) ) {
                    $data['status'] = 'Please fill out both inputs.';
                }else {
                    // then create a new row in the table
                    $db->create('posts',$article);
                    $data['status'] = 'Row has successfully been inserted.';
                }
            }
            $layout->view('admin/create', $data);
            break;
        case 'edit':
            $id   = $_REQUEST['id'];
            $data['status'] =null;

            if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {

                $article               = array();
                $article['title']      = $_POST['title'];
                $article['html']    = Markdown::defaultTransform($_POST['content']);
                $article['content']    =$_POST['content'];
                $article['saved_at'] = new MongoDate();

                if ( empty($article['title']) || empty($article['content']) ) {
                    $data['status'] = 'Please fill out both inputs.';
                }else {
                    // then create a new row in the table
                    $db->update($id,'posts',$article);
                    $data['status'] = 'Row has successfully been update.';
                }
            }
           $layout->view('admin/edit',array(
                'article' => $db->getById($id,'posts'),
                'status'  => $data['status']
            ));
            break;
        case 'delete':
            $id = $_GET['id'];
            $status = $db->delete($id,'posts');
            if ($status ==TRUE ) {
                header("Location:index");
            }
            break;
        default:
            $currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
            $data = $db->get($currentPage,'posts');


            $layout->view('admin/dashboard',array(
                'currentPage'  => $data[0],
                'totalPages'   => $data[1],
                'cursor'       => $data[2],

            ));
        break;
    }
}

index.php位于文件夹中,即../admin/index.php,db.php位于网络目录的根文件夹中。

  

致命错误:调用未定义的方法DB :: get()in   第82行的C:\ xampp \ htdocs \ admin \ index.php。

     

错误行:$ data = $ db-&gt; get($ currentPage,&#39; posts&#39;);

2 个答案:

答案 0 :(得分:0)

我无法在index.php中看到您创建调用数据库构造函数的$db对象的位置。

至少在代码的开头需要一行代码:

$db = new DB($config);

答案 1 :(得分:0)

嗯,我刚解决了。我没有在app1.php中调用构造函数并将其包含在索引中,而是直接在索引本身中调用它。现在它的工作。

感谢回复人员!