用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4258|回复: 5
收起左侧

[HELP] 我想要知道自定义的对象,怎么在CI里面方便的使用

[复制链接]
发表于 2012-2-2 21:49:35 | 显示全部楼层 |阅读模式
以前习惯了自己随便定义对象来使用,里面有构造方法,get,set方法之类的,但是MVC模式下的CI框架的model层的对象貌似不能够在初始化的时候调用构造函数传入数据进去,先贴我以前的代码:
PHP复制代码
 
<?php
    class Exercise {
        protected $id;
        protected $content;
        protected $answer;
        protected $ischecked;
        protected $e_difficulty_id;
        protected $er_knowledgepoint_id;
        protected $e_exercisetype_id;
        protected $er_teacher_id;
        protected $e_subject_id;
        public function __construct($data) {
            foreach ($data as $key => $value) {
                $this->$key = $value;
            }
        }
   
        public function __get($property_name) {
            if (isset($this->$property_name)) {
                return ($this->$property_name);
            } else {
                return (NULL);
            }
        }
   
        public function __set($property_name, $value) {
            $this->$property_name = $value;
        }
    }
 
复制代码

以前在创建对象的时候直接$exercise = new Exercise($data);对象就创建好了,但是现在用CI,我想要直接把这样的代码放到model层去,全部放到一个对象里面进行操作,而且在Controller层也可以直接用到这一个对象来进行操作。
现在我的方法是把这些自定义的类的代码专门放到一个目录下,然后在Controller中include进去,这样就可以new 我的对象了。。但是这样做就不能很好的体现CI的model的用处了。感觉CI的model是面向过程的,视频教程里面的那些也是基本都是在model里面直接写的方法然后调用,都没看到在model里面初始化数据,面向对象的进行编程。。。
发表于 2012-2-3 01:40:08 | 显示全部楼层
CI是自动初始化的,你可能需要定义个init($data)函数做你的构造函数的事情。
就是model的代码为
PHP复制代码
 
        function __construct() {
                parent::__construct();
        }
        function init($data) {
            foreach ($data as $key => $value) {
                $this->$key = $value;
            }
        }
 
复制代码

然后调用的时候
$this->exercise->init($data)

不知道你是否需要这样一个结果。
发表于 2012-2-3 13:00:28 | 显示全部楼层
model
PHP复制代码
 
<?php
    class Exercise extends CI_Model {
        protected $id;
        protected $content;
        protected $answer;
        protected $ischecked;
        protected $e_difficulty_id;
        protected $er_knowledgepoint_id;
        protected $e_exercisetype_id;
        protected $er_teacher_id;
        protected $e_subject_id;
        public function __construct($data) {
            parent::__construct();
            foreach ($data as $key => $value) {
                $this->$key = $value;
            }
        }
   
        public function __get($property_name) {
            if (isset($this->$property_name)) {
                return ($this->$property_name);
            } else {
                return (NULL);
            }
        }
   
        public function __set($property_name, $value) {
            $this->$property_name = $value;
        }
    }
 
复制代码

controller
PHP复制代码
 
class Exercisemanager extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('exercise');
    }
    public function index() {
        $this->exercise->__construct();
        //some code deal with exercise
    }
}
 
复制代码
发表于 2012-2-3 13:39:36 | 显示全部楼层
把你的对象改造成类库或者是模型。
你有两条路:
1. 改造成 CI 的方式。
2. 直接 include

建议改造成 CI 的方式。
 楼主| 发表于 2012-2-3 20:35:18 | 显示全部楼层
呵呵,谢谢大神们的指点~几种方法都行啊~
发表于 2014-5-16 17:35:31 | 显示全部楼层
我使用的是扩展model的办法
在core下面新建MY_Loader.php
PHP复制代码
 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Model Class
 * @author      57sy.com QQ:821200318
 */

class MY_Loader extends CI_Loader
{
 
        /**
         * Model Loader  扩展CI的model
         *
         * This function lets users load and instantiate models.
         *
         * @param       string  the name of the class
         * @param       string  name for the model
         * @param       bool    database connection
         * @param       string  array('type'=>'real_data') 调用哪个数据库, 此处还可以传递别的参数,增加即可
         * @return      void
         */

        public function model($model, $name = '', $db_conn = FALSE , $type_str = array('type'=>'real_data') )
        {
                if (is_array($model))
                {
                        foreach ($model as $babe)
                        {
                                $this->model($babe);
                        }
                        return;
                }
 
                if ($model == '')
                {
                        return;
                }
 
                $path = '';
 
                // Is the model in a sub-folder? If so, parse out the filename and path.
                if (($last_slash = strrpos($model, '/')) !== FALSE)
                {
                        // The path is in front of the last slash
                        $path = substr($model, 0, $last_slash + 1);
 
                        // And the model name behind it
                        $model = substr($model, $last_slash + 1);
                }
 
                if ($name == '')
                {
                        $name = $model;
                }
 
                if (in_array($name, $this->_ci_models, 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);
                }
 
                $model = strtolower($model);
 
                foreach ($this->_ci_model_paths as $mod_path)
                {
                        if ( ! file_exists($mod_path.'models/'.$path.$model.'.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_Model'))
                        {
                                load_class('Model', 'core');
                        }
 
                        require_once($mod_path.'models/'.$path.$model.'.php');
 
                        $model = ucfirst($model);
 
                        $CI->$name = new $model($type_str);
 
                        $this->_ci_models[] = $name;
                        return;
                }
 
                // couldn't find the model
                show_error('Unable to locate the model you have specified: '.$model);
        }
       
}
 
 
复制代码

本版积分规则