Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter抽象基模型_Php_Codeigniter_Model_Abstract - Fatal编程技术网

Php Codeigniter抽象基模型

Php Codeigniter抽象基模型,php,codeigniter,model,abstract,Php,Codeigniter,Model,Abstract,是否可以在Codeigniter中实现抽象的基础模型?我已经创建了一个具有基本CRUD功能的抽象类,但是我收到了一个“无法实例化抽象类”错误 抽象模型位于/application/core/my_model.php中,并且非常简单 abstract class MY_Model extends CI_Model { 无法实例化抽象类”位于/system/core/Common.php的第174行 看起来Codeigniter在加载时试图实例化my_model.php,我猜这是因为/core文件

是否可以在Codeigniter中实现抽象的基础模型?我已经创建了一个具有基本CRUD功能的抽象类,但是我收到了一个“无法实例化抽象类”错误

抽象模型位于/application/core/my_model.php中,并且非常简单

abstract class MY_Model extends CI_Model
{
无法实例化抽象类”位于/system/core/Common.php的第174行

看起来Codeigniter在加载时试图实例化my_model.php,我猜这是因为/core文件夹中的文件被用作扩展核心系统文件(如控制器和模型)的位置。有什么方法可以阻止这种情况吗?我试图使用自动加载模型,但没有帮助

/*
| -------------------------------------------------------------------
|  Native Auto-load
| -------------------------------------------------------------------
| 
| Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
*/

function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'core/'. $class . EXT );
    }
}

我意识到一个简单的方法就是将文件放在我想使用的每个模型的顶部,但这显然不是最理想的。

为什么要将我的模型抽象化?你只需将所有CRUD函数放在我的模型中,并从我的模型而不是CI模型扩展你的模型。也不需要使用自动加载s CodeIgniter允许您使用MY_模型扩展CI_模型,只要您将其放入核心文件夹

下面是一个很好的MY_模型示例,来自Jamie Rumie。您可以在这里找到它:

祝你好运

干杯


Bart

我试图调整Loader类,使其接受摘要和接口

首先,我将所有编辑内容放入my/application/core/my_Loader.php:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package     CodeIgniter
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license     http://codeigniter.com/user_guide/license.html
 * @link        http://codeigniter.com
 * @since       Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------
// Joseff Betancourt - 11/14/2012 modified code by adding a autoloader and interface loader copied from model loader.

/**
 * Loader Class
 *
 * Loads views and files
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @author      ExpressionEngine Dev Team
 * @category    Loader
 * @link        http://codeigniter.com/user_guide/libraries/loader.html
 */
class MY_Loader extends CI_Loader {

    // All these are set automatically. Don't mess with them.
    /**
     * Nesting level of the output buffering mechanism
     *
     * @var int
     * @access protected
     */

    protected $_ci_abstracts_paths      = array();
    /**
     * List of paths to load models from
     *
     * @var array
     * @access protected
     */
    protected $_ci_interfaces_paths     = array();
    /**
     * List of paths to load helpers from
     *
     * @var array
     * @access protected
     */

    protected $_ci_abstracts            = array();
    /**
     * List of loaded interfaces
     *
     * @var array
     * @access protected
     */
    protected $_ci_interfaces           = array();
    /**
     * List of loaded helpers
     *
     * @var array
     * @access protected
     */

    /**
     * Constructor
     *
     * Sets the path to the view files and gets the initial output buffering level
     */

    function __construct()
    {
        parent::__construct();
        $this->_ci_abstracts_paths = array(APPPATH);
        $this->_ci_interfaces_paths = array(APPPATH);
        log_message('debug', "Loader Class Initialized");
    }

    // --------------------------------------------------------------------

    /**
     * Initialize the Loader
     *
     * This method is called once in CI_Controller.
     *
     * @param   array
     * @return  object
     */
    public function initialize()
    {

        $this->_ci_abstracts = array();
        $this->_ci_interfaces = array();
        $this->_ci_autoloader();

        return $this;
    }

    // --------------------------------------------------------------------

    /**
     * Abstracts Loader
     *
     * This function lets users load and instantiate models.
     *
     * 11/14/2012 - Joseff Betancourt - Cloned from Models
     *
     * @param   string  the name of the class
     * @param   string  name for the abstract
     * @param   bool    database connection
     * @return  void
     */
    public function abstracts($abstracts, $name = '', $db_conn = FALSE)
    {
        if (is_array($abstracts))
        {
            foreach ($abstracts as $babe)
            {
                $this->abstracts($babe);
            }
            return;
        }

        if ($abstracts == '')
        {
            return;
        }

        $path = '';

        // Is the abstracts in a sub-folder? If so, parse out the filename and path.
        if (($last_slash = strrpos($abstracts, '/')) !== FALSE)
        {
            // The path is in front of the last slash
            $path = substr($abstracts, 0, $last_slash + 1);

            // And the model name behind it
            $abstracts = substr($abstracts, $last_slash + 1);
        }

        if ($name == '')
        {
            $name = $abstracts;
        }

        if (in_array($name, $this->_ci_abstracts, TRUE))
        {
            return;
        }

        $CI =& get_instance();
        if (isset($CI->$name))
        {
            show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
        }

        $abstracts = strtolower($abstracts);

        foreach ($this->_ci_abstracts_paths as $mod_path)
        {
            if ( ! file_exists($mod_path.'abstracts/'.$path.$abstracts.'.php'))
            {
                continue;
            }

            if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
            {
                if ($db_conn === TRUE)
                {
                    $db_conn = '';
                }

                $CI->load->database($db_conn, FALSE, TRUE);
            }

            if ( ! class_exists('CI_Abstracts'))
            {
                load_class('Abstracts', 'core');
            }

            require_once($mod_path.'abstracts/'.$path.$abstracts.'.php');

            $abstracts = ucfirst($abstracts);

            $CI->$name = new $abstracts();

            $this->_ci_abstracts[] = $name;
            return;
        }

        // couldn't find the abstracts
        show_error('Unable to locate the abstracts you have specified: '.$abstracts);
    }

    // --------------------------------------------------------------------

    /**
     * Interface Loader
     *
     * This function lets users load and instantiate interfaces.
     *
     * 11/14/2012 - Joseff Betancourt - Cloned from Models
     *
     * @param   string  the name of the class
     * @param   string  name for the interface
     * @param   bool    database connection
     * @return  void
     */
    public function interfaces($interfaces, $name = '', $db_conn = FALSE)
    {
        if (is_array($interfaces))
        {
            foreach ($interfaces as $babe)
            {
                $this->interfaces($babe);
            }
            return;
        }

        if ($interfaces == '')
        {
            return;
        }

        $path = '';

        // Is the abstracts in a sub-folder? If so, parse out the filename and path.
        if (($last_slash = strrpos($interfaces, '/')) !== FALSE)
        {
            // The path is in front of the last slash
            $path = substr($interfaces, 0, $last_slash + 1);

            // And the model name behind it
            $interfaces = substr($interfaces, $last_slash + 1);
        }

        if ($name == '')
        {
            $name = $interfaces;
        }

        if (in_array($name, $this->_ci_interfaces, TRUE))
        {
            return;
        }

        $CI =& get_instance();
        if (isset($CI->$name))
        {
            show_error('The interface name you are loading is the name of a resource that is already being used: '.$name);
        }

        $interfaces = strtolower($interfaces);

        foreach ($this->_ci_interfaces_paths as $mod_path)
        {
            if ( ! file_exists($mod_path.'interfaces/'.$path.$interfaces.'.php'))
            {
                continue;
            }

            if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
            {
                if ($db_conn === TRUE)
                {
                    $db_conn = '';
                }

                $CI->load->database($db_conn, FALSE, TRUE);
            }

            if ( ! class_exists('CI_Interfaces'))
            {
                load_class('Interfaces', 'core');
            }

            require_once($mod_path.'interfaces/'.$path.$interfaces.'.php');

            $interfaces = ucfirst($interfaces);

            $CI->$name = new $interfaces();

            $this->_ci_interfaces[] = $name;
            return;
        }

        // couldn't find the interfaces
        show_error('Unable to locate the interfaces you have specified: '.$interfaces);
    }

    // --------------------------------------------------------------------


    /**
     * Autoloader
     *
     * The config/autoload.php file contains an array that permits sub-systems,
     * libraries, and helpers to be loaded automatically.
     *
     * @param   array
     * @return  void
     */
    private function _ci_autoloader()
    {
        // Abstracts models
        if (isset($autoload['abstracts']))
        {
            $this->model($autoload['abstracts']);
        }

        // Interfaces models
        if (isset($autoload['interfaces']))
        {
            $this->model($autoload['interfaces']);
        }

    }

    // --------------------------------------------------------------------

}

/* End of file Loader.php */
/* Location: ./system/core/Loader.php */
最后,我在应用程序文件夹中添加了一个目录,用于摘要和界面。虽然还没有完全验证,但我认为这是一种更全面的方法,允许用户在需要引用时加载摘要

您还可以在抽象文件夹中创建一个抽象,如下所示:

abstract class MY_Model extends CI_Model
{

blah 

}

将文件保存在/application/core/MY_model.php中:

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

class MY_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    // Return all records in the table
    public function get_all($table)
    {
        $q = $this->db->get($table);
        if($q->num_rows() > 0)
        {
            return $q->result();
        }
        return array();
    }

    // Return only one row
    public function get_row($table,$primaryfield,$id)
    {
        $this->db->where($primaryfield,$id);
        $q = $this->db->get($table);
        if($q->num_rows() > 0)
        {
            return $q->row();
        }
        return false;
    }

    // Return one only field value
    public function get_data($table,$primaryfield,$fieldname,$id)
    {
        $this->db->select($fieldname);
        $this->db->where($primaryfield,$id);
        $q = $this->db->get($table);
        if($q->num_rows() > 0)
        {
            return $q->result();
        }
        return array();
    }

    // Insert into table
    public function add($table,$data)
    {
        return $this->db->insert($table, $data);
    }

    // Update data to table
    public function update($table,$data,$primaryfield,$id)
    {
        $this->db->where($primaryfield, $id);
        $q = $this->db->update($table, $data);
        return $q;
    }

    // Delete record from table
    public function delete($table,$primaryfield,$id)
    {
        $this->db->where($primaryfield,$id);
        $this->db->delete($table);
    }

    // Check whether a value has duplicates in the database
    public function has_duplicate($value, $tabletocheck, $fieldtocheck)
    {
        $this->db->select($fieldtocheck);
        $this->db->where($fieldtocheck,$value);
        $result = $this->db->get($tabletocheck);

        if($result->num_rows() > 0) {
            return true;
        }
        else {
            return false;
        }
    }

    // Check whether the field has any reference from other table
    // Normally to check before delete a value that is a foreign key in another table
    public function has_child($value, $tabletocheck, $fieldtocheck)
    {
        $this->db->select($fieldtocheck);
        $this->db->where($fieldtocheck,$value);
        $result = $this->db->get($tabletocheck);

        if($result->num_rows() > 0) {
            return true;
        }
        else {
            return false;
        }
    }

    // Return an array to use as reference or dropdown selection
    public function get_ref($table,$key,$value,$dropdown=false)
    {
        $this->db->from($table);
        $this->db->order_by($value);
        $result = $this->db->get();

        $array = array();
        if ($dropdown)
            $array = array("" => "Please Select");

        if($result->num_rows() > 0) {
            foreach($result->result_array() as $row) {
            $array[$row[$key]] = $row[$value];
            }
        }
        return $array;
    }
}

是我编写的代码。希望它能有所帮助,人们可以为我搞砸的任何事情做出贡献。只是澄清一下……为了让它在CI上工作,你真的必须破解(或扩展)核心?FML,为什么不只是使用FuelPHP…?,或者一些真正支持常见OOP实践的东西?为什么要付出这么多努力?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    // Return all records in the table
    public function get_all($table)
    {
        $q = $this->db->get($table);
        if($q->num_rows() > 0)
        {
            return $q->result();
        }
        return array();
    }

    // Return only one row
    public function get_row($table,$primaryfield,$id)
    {
        $this->db->where($primaryfield,$id);
        $q = $this->db->get($table);
        if($q->num_rows() > 0)
        {
            return $q->row();
        }
        return false;
    }

    // Return one only field value
    public function get_data($table,$primaryfield,$fieldname,$id)
    {
        $this->db->select($fieldname);
        $this->db->where($primaryfield,$id);
        $q = $this->db->get($table);
        if($q->num_rows() > 0)
        {
            return $q->result();
        }
        return array();
    }

    // Insert into table
    public function add($table,$data)
    {
        return $this->db->insert($table, $data);
    }

    // Update data to table
    public function update($table,$data,$primaryfield,$id)
    {
        $this->db->where($primaryfield, $id);
        $q = $this->db->update($table, $data);
        return $q;
    }

    // Delete record from table
    public function delete($table,$primaryfield,$id)
    {
        $this->db->where($primaryfield,$id);
        $this->db->delete($table);
    }

    // Check whether a value has duplicates in the database
    public function has_duplicate($value, $tabletocheck, $fieldtocheck)
    {
        $this->db->select($fieldtocheck);
        $this->db->where($fieldtocheck,$value);
        $result = $this->db->get($tabletocheck);

        if($result->num_rows() > 0) {
            return true;
        }
        else {
            return false;
        }
    }

    // Check whether the field has any reference from other table
    // Normally to check before delete a value that is a foreign key in another table
    public function has_child($value, $tabletocheck, $fieldtocheck)
    {
        $this->db->select($fieldtocheck);
        $this->db->where($fieldtocheck,$value);
        $result = $this->db->get($tabletocheck);

        if($result->num_rows() > 0) {
            return true;
        }
        else {
            return false;
        }
    }

    // Return an array to use as reference or dropdown selection
    public function get_ref($table,$key,$value,$dropdown=false)
    {
        $this->db->from($table);
        $this->db->order_by($value);
        $result = $this->db->get();

        $array = array();
        if ($dropdown)
            $array = array("" => "Please Select");

        if($result->num_rows() > 0) {
            foreach($result->result_array() as $row) {
            $array[$row[$key]] = $row[$value];
            }
        }
        return $array;
    }
}
class any_model extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
}