Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

php - CodeIgniter, models, and ORM, how to deal with this?

I'm starting with CodeIgniter and after several hours diving in Google I'm a bit confused.

Let's try to explain my question with a easy example: I have a table 'car' with the fields 'name' and 'color'. Therefore I want to have a php class Car, so that my code could look finally like this:

$car = new Car('BMW', 'red'); //new $car Object
$car->save(); //this will make an SQL insert to the table 'car'

//Lets query all cars
$cars = Car::get_all(); 
//cars will be an array of Car objects, (not a set of rows!)

Therefore, I am looking for something pretty similar to what you have in RubyOnRails or Django (Python). I need to handle all kind of relationships, and to be able of write code in a true OOP+MVC way.

These are my failed approaches to get it:

Using an external ORM (DataMapper, Doctrine, AcidCrud...)

They either requires too many settings, or they handle relationships in a poor way.

Using CodeIgniter classes (to extend the CodeIgniter's Model class)

class Car extends Model{
public function Car($name='',$color='')
{
    $this->name     =   $name;
    $this->color    =   $color;      
    parent::Model();
}
public function save()
{
    $data = array(
                   'name'   =>  $this->name ,
                   'color'  =>  $this->color 
                  );

    $this->db->insert('cars' $data);
}

And so on... Problem with this approach is that if a do a var_dump() of a $car object, I see that it contains a lot of stuff from the CodeIgniter, such as the objects CI_Config, CI_Input, CI_Benchmark, etc. Therefore I think this is not a good solution, because each object of my class Car, it will contain a lot of repeated data, (it will have a poor performance!), isn't it?

Not using the CodeIgniter's models

I could make my models without extending them from CodeIgniter's Model class, and then using the regular PHP5 constructor (__construct() instead of function Car()), but problem in this case is: how I access to the $db object to make querys using the CodeIgniter's ActiveRecord? and, how I load the models (its classes) within the controllers?

question from:https://stackoverflow.com/questions/2141007/codeigniter-models-and-orm-how-to-deal-with-this

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You probably want something like this:

   class Cars {

    //List all neccessary vars here

    function __construct() {
        //get instance of Codeigniter Object and load database library
        $this->obj =& get_instance();
        $this->obj->load->database();
    }

//You can now list methods like so:
function selectCar($name, $color) {

        $this->obj->db->select('color')->from('car')->where('color', $color);
        $query = $this->obj->db->get();

        switch ($query->num_rows()) {
        case 0:
            return false;
            break;
        default:
            return $query->result();
            break;
        }
    }

Hope that helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...