CodeIgniter,模型和ORM,怎么处理这个? [英] CodeIgniter, models, and ORM, how to deal with this?
问题描述
我从CodeIgniter开始,经过几个小时的潜水在谷歌我有点困惑。
让我们以一个简单的例子来解释我的问题:我有一个'car'字段'name'和'color'。因此,我想有一个php类Car,所以我的代码可以看起来像这样:
$ car = new Car '宝马','红'); // new $ car Object
$ car-> save(); // this will make an SQL insert to the table'car'
//让查询所有的汽车
$ cars = Car :: get_all();
//汽车将是一个Car对象数组(不是一组行!)
因此,我寻找的东西非常类似于你在RubyOnRails或Django(Python)。我需要处理所有类型的关系,并能够以真正的OOP + MVC方式编写代码。
这些是我失败的方法来获取它: p>
使用外部ORM (DataMapper,Doctrine,AcidCrud ...)
使用CodeIgniter类(扩展CodeIgniter的Model类)
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);
}
等等...这种方法的问题是,一个$ car对象的var_dump(),我看到它包含了很多来自CodeIgniter的东西,比如对象CI_Config,CI_Input,CI_Benchmark等。因此我认为这不是一个好的解决方案,因为我的类的每个对象汽车,它将包含很多重复的数据,(它将有一个糟糕的性能!),不是吗?
不使用CodeIgniter的模型
我可以使我的模型不需要扩展CodeIgniter的Model类,然后使用正则PHP5构造函数(__construct(),而不是函数Car()但在这种情况下的问题是:我如何访问$ db对象使用CodeIgniter的ActiveRecord进行查询?
你可能想要这样的东西:
class Cars {
//列出所有必要的变量
function __construct ){
//获取Codeigniter对象的实例并加载数据库库
$ this-> obj =& get_instance();
$ this-> obj-> load-> database();
}
//现在可以列出如下方法:
function selectCar($ name,$ color){
$ this-> obj-> db-> select('color') - > from('car') - >其中('color',$ color)
$ query = $ this-> obj-> db-> get();
switch($ query-> num_rows()){
case 0:
return false;
break;
默认值:
return $ query-> result();
break;
}
}
希望有所帮助!
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?
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!
这篇关于CodeIgniter,模型和ORM,怎么处理这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!