Codeigniter模型只是实用程序类? [英] Codeigniter models are just utility classes?

查看:126
本文介绍了Codeigniter模型只是实用程序类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我习惯的MVC中,模型类(通常)表示表,这些类的对象是行/域对象。我不明白在CodeIgniter为什么模型类似乎只是单例的实用程序类。感觉写错了

In the MVC that I'm accustomed to, model classes (usually) represent tables and objects of these classes are rows/domain objects. I don't understand in CodeIgniter why model classes appear to just be singleton utility classes. It feels wrong writing

$data = array('text' => 'hello');
$this->commentModel->insert($data);

而不是

$comment = new Comment();
$comment->text = 'hello';
$comment->save();



< (或告诉我我可以做些什么来解决它。)

Can someone explain why CodeIgniter does models this way and make me feel better about it? (Or tell me what I can do to fix it.)

推荐答案

CodeIgniter中的模型是使用单例模式。虽然这似乎让很多人习惯使用更多的PHP OOP方法,这有点混乱。有几个原因。

Models in CodeIgniter are designed using singleton pattern you're right. While this seems confusing to many people who are used to working with a more PHP OOP approach, there are a few reasons.


简单的是,你可以
加载一个模型只是一次,并有它
可用在超级全局使用
在整个系统。

The first most simple is that you can load a model just the once and have it available in the super-global for use throughout the system.

这是唯一真正的加号,其余的是歉意的解释。

That's the only real plus here, the rest are apologetic explanations.


2006年以PHP 4
支持为主要优先。

CI was built in 2006 with PHP 4 support as a main priority.

这只是刚刚开始改变EllisLab已经删除了PHP 4

This is only just starting to change now EllisLab has dropped PHP 4 support from CI 2.0, but for now, that's how the framework works.

您当然可以加载模型,然后使用您喜欢的任何PHP 5语法您的模型。 / p>

You can of course load a model then use whatever PHP 5 syntax you like for your models.


$ this-> load-> model('comment');

$this->load->model('comment');

$ comment = new Comment();
$ comment-> text ='hello';
$ comment-> save();

$comment = new Comment(); $comment->text = 'hello'; $comment->save();

这篇关于Codeigniter模型只是实用程序类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆