Tao.Yu 发表于 2012-2-2 21:49:35

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

以前习惯了自己随便定义对象来使用,里面有构造方法,get,set方法之类的,但是MVC模式下的CI框架的model层的对象貌似不能够在初始化的时候调用构造函数传入数据进去,先贴我以前的代码:

<?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里面初始化数据,面向对象的进行编程。。。

acabin 发表于 2012-2-3 01:40:08

CI是自动初始化的,你可能需要定义个init($data)函数做你的构造函数的事情。
就是model的代码为

      function __construct() {
                parent::__construct();
      }
      function init($data) {
            foreach ($data as $key => $value) {
                $this->$key = $value;
            }
      }

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

不知道你是否需要这样一个结果。

yunnysunny 发表于 2012-2-3 13:00:28

model

<?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

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
    }
}

Hex 发表于 2012-2-3 13:39:36

把你的对象改造成类库或者是模型。
你有两条路:
1. 改造成 CI 的方式。
2. 直接 include

建议改造成 CI 的方式。

Tao.Yu 发表于 2012-2-3 20:35:18

呵呵,谢谢大神们的指点~几种方法都行啊~

57sy.com(隐身中 发表于 2014-5-16 17:35:31

我使用的是扩展model的办法
在core下面新建MY_Loader.php

<?phpif ( ! 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);
        }
       
}


页: [1]
查看完整版本: 我想要知道自定义的对象,怎么在CI里面方便的使用