php - 与 CodeIgniter Controller 、模型和模型工厂相关的两个错误

标签 php codeigniter class

我在使用 CodeIgniter 进行操作时遇到了一些问题,作为 CodeIgniter 新手和 php 业余爱好者,我很难看到代码中的问题。

我按照教程将所有这些组合在一起。该教程有用户而不是评论,但我非常密切地关注并认为我的一切都是正确的,尽管我还没有完全理解所有内容。

这是模型,review_model.php 位于 models 目录

class Review_Model extends CI_Model {

    /*
    * a private variable to represent each variable in the database
    */
    private $_id; // int

    function __construct() {
        parent::__construct();
    }

    /*
    * SETs and GETs
    * sets and gets allow you to retrieve or set a private variable on an object
    */

    /**
    * @return int [$this->_id] Return this review's ID
    */
    public function getId() {
        return $this->_id;
    }

    /**
    * @param int Integer to set this review's ID to
    */
    public function setId($value) {
        $this->_id = $value;
    }

    /*
    * Class Methods
    */

    /**
    *   Commit method. This will commit the entire object to the database
    */

    public function commit() {
        $data = array(
                'title' => $this->_title,
                'date' => $this->_date,
                'directors' => $this->_directors,
                'writers' => $this->_writers,
                'cast' => $this->_cast,
                'genre' => $this->_genre,
                'runtime' => $this->_runtime,
                'blurb' => $this->_blurb,
                'slug' => $this->_slug,
                'synopsis' => $this->_synopsis,
                'review' => $this->_review,
                'where_to_find' => $this->_where_to_find,
                'banner' => $this->_banner,
                'images' => $this->_images,
                'videos' => $this->_videos,
                'rating' => $this->_rating
        );

        if ($this->_id > 0) {
            // We have an ID so we need to update this review because it is not new
            if ($this->db->update("reviews", $data, array("id" => $this->_id))) {
                return true;
            }
        } else {
            // We dib;t have an ID meaning it is new and not yet in the database so we need to do an insert
            if ($this->db->insert("reviews", $data)) {
                // Now we can get the ID and update te newly created object
                $this->_id = $this->db->insert_id();
                return true;
            }
        }
        return false;
    }
}

这是模型工厂,libraries 目录中的 ReviewFactory.php

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

class ReviewFactory {

    private $_ci;

    function __construct() {
        // When the class is contructed get an instance of CodeIgniter so we can access it locally
        $this->_ci =& get_instance();
        // Include the review_model so we can use it
        $this->_ci->load->model("review_model");
    }

    public function getReview($id = 0) {
        // Are we getting a single review or are we getting them all?
        if ($id > 0) {
            // Getting a single review
            $query = $this->_ci->db->get_where("reviews", array("id" => $id));
            // Check if any results were returned
            if ($query->num_rows() > 0) {
                // Pass the data to our local function to create an object for us and return this new object
                return $this->createObjectFromData($query->row());
            }
            return false;
        } else {
            // Getting all the reviews
            $query = $this->_ci->db->select("*")->from("reviews")->get();
            // Check if any results were returned
            if ($query->num_rows() > 0) {
                // Create an array to store reviews
                $reviews = array();
                // Loop through each row returned from the query
                foreach ($query->result() as $row) {
                    // Pass the row data to our local function which creates a new user object with the data provided and add it to the reviews array
                    $reviews[] = $this->createObjectFromData($row);
                }
                // Return the reviews array
                return $reviews;
            }

            return false;
        }
    }
}

public function createObjectFromData($row) {
    // Create a new review_model object
    $review = new Review_Model();
    // Set the ID on the review model
    $review->setId($row->id);


    // return the new review on the object
    return $review;
}

这是 Controller ,reviews.php 在 Controller 目录中

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

class Reviews extends CI_Controller {

    public function index() {
        echo "This is the index!";
    }

    public function reviews($reviewId = 0) {
        // Always ensure an integer
        $reviewId = (int)$reviewId;
        // Load the review factory
        $this->load->library("ReviewFactory");
        // Create a data array so we can pass information to the view
        $data = array(
            "reviews" => $this->reviewfactory->getReview($reviewId)
        );
        // Load the view and pass the data to it
        $this->load->view("reviews", $data);
    }
}

最后,这是我遇到的两个错误。

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Reviews::$load

Filename: controllers/reviews.php

Line Number: 15

Backtrace:

File: D:\wamp\www\application\controllers\reviews.php
Line: 15
Function: _error_handler

File: D:\wamp\www\sn\index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Error

Message: Call to a member function library() on a non-object

Filename: controllers/reviews.php

Line Number: 15

Backtrace:

最佳答案

这应该不重要,但尝试将其添加到您的 Controller 中只是为了排除它:

public function __construct()
{
    parent::__construct();
}

此外,修复您的文件命名:

Review_model.php Reviewfactory.php 评论.php

干杯!

关于php - 与 CodeIgniter Controller 、模型和模型工厂相关的两个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32238060/

相关文章:

php - 插入数据库时​​如何获取ID

javascript - 在表行中搜索列和突出显示中的特定文本

php - 对象和对象元模式设计

php - 如何在 Codeigniter Active 记录中编写 MYSQL 查询 !=?

java - 将 Class<?> cls 作为方法参数传递?

javascript:需要按下按钮两次才能触发onclick

php - 如何知道 key 是否存在于 Json 字符串中

php - 在 codeigniter 中启用 $_GET

php - 检查一个类是否是另一个类的子类

javascript - Jquery 将 css 样式应用于除鼠标悬停图像之外的具有特定类的所有元素